Azzera filtri
Azzera filtri

importing data from .csv files of different lengths and same columns in matlab and getting RMS.

3 visualizzazioni (ultimi 30 giorni)
I have a displacement data in 73 .csv files which are comma separated with 7 (fixed) columns and rows (varying) from 1500 to 9000 rows depending on the file. Each file contains 7 Columns as x y z(coordinates of voxels) and u v w(displacements in u v w directions) and sigma in 7th column. I want to create a code to read the .csv files and get the rms error in u v w directions respectively. RMS formula has to applied and i have to get 1 rms value for all the files combined and that too it should consider all the values of u v w in all the files.Can someone help me with the idea.I have attached the code which i used which i not working. I attached some files of the format .csv in the attachments.
data0001 = csvread('C://Users//User//Desktop//.csv//data_0001.csv');
.
.
.
.
.
data0071 = csvread('C://Users//User//Desktop//.csv//data_0071.csv');
data0072 = csvread('C://Users//User//Desktop//.csv//data_0072.csv');
data0073 = csvread('C://Users//User//Desktop//.csv//data_0073.csv');
%Root Mean Squared Error
for ii= 01:01:73
erroru{ii} = (sum((data00ii(:,4)).^2)); %error in u
errorv{ii} = (sum((data00ii(:,5)).^2)); %error in v
errorw{ii} = (sum((data00ii(:,6)).^2)); %error in w
U=erroru{ii}/(length(data00ii));
V=errorv{ii}/(length(data00ii));
W=errorw{ii}/(length(data00ii));
S= sum(U+V+W);
rmsError{ii} = sqrt(S);
show(rmsError{ii})
end

Risposte (1)

Nihal
Nihal il 13 Giu 2024
Your approach has the right intent, but there are several issues to address for it to work correctly. The key problems include the dynamic variable names and the incorrect calculation of RMS over all files combined. Here's a revised approach that should meet your requirements:
  1. Dynamic File Reading: Use a loop to read each file without manually specifying the file names.
  2. Accumulate Displacement Squares: Accumulate the squares of displacements (u, v, w) across all files before calculating the RMS.
  3. Correct RMS Calculation: Calculate RMS correctly over all files combined.
Here's how you could write the MATLAB code:
% Define the base path and pattern for the file names
basePath = '';
filePattern = fullfile(basePath, 'data_*.csv');
% Get a list of all CSV files matching the pattern
csvFiles = dir(filePattern);
% Initialize accumulators for the squares of u, v, w displacements
sumSqU = 0;
sumSqV = 0;
sumSqW = 0;
totalNumRows = 0;
% Loop over each file
for k = 1:length(csvFiles)
% Construct full file name and read the file
fullFileName = fullfile(basePath, csvFiles(k).name);
data = readtable(fullFileName);
% Accumulate the squares of displacements and the total row count
sumSqU = sumSqU + sum(data(:,4).^2);
sumSqV = sumSqV + sum(data(:,5).^2);
sumSqW = sumSqW + sum(data(:,6).^2);
totalNumRows = totalNumRows + size(data, 1);
end
% Calculate the RMS error for u, v, w combined
rmsErrorU = sqrt(sumSqU.u_voxel_/totalNumRows);
rmsErrorV = sqrt(sumSqV.v_voxel_/totalNumRows);
rmsErrorW = sqrt(sumSqW.w_voxel_/totalNumRows);
% Calculate the combined RMS error for all directions
combinedRmsError = sqrt((sumSqU.u_voxel_ + sumSqV.v_voxel_ + sumSqW.w_voxel_) / totalNumRows);
% Display the results
fprintf('RMS Error in U direction: %f\n', rmsErrorU);
fprintf('RMS Error in V direction: %f\n', rmsErrorV);
fprintf('RMS Error in W direction: %f\n', rmsErrorW);
fprintf('Combined RMS Error: %f\n', combinedRmsError);
This script dynamically reads all the .csv files in the specified directory, calculates the sum of squares for each displacement direction across all files, and then computes the RMS error for each direction individually and combined. This approach avoids the need to manually specify each file and correctly aggregates the displacement errors across all files before calculating the RMS, providing a more accurate overall error metric.

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Tag

Prodotti


Release

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by