How to import image CSV data into matlab and show it and make Calculation automatically

5 views (last 30 days)
Dear all,
I would like to ask you a question about the CSV data loop. I have nearly 100 csv Data sheet, and i would like to make a calcuation based on all of it. For example, I have A1,A2, A3, A4,...... and B1, B2,B3, i would like to do the calculation A1/(A1+B1), A2/(A2+B2)........and so on.
this is the code i wrote
% Specify the folder where the files live.
myFolder = '\\\Desktop\data\1_1-6';
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.csv'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
end
Data=readmatrix(fullFileName); % this is only show the last csv values, not all of them.
  1. I would like to save this 100 CSV in matlab individually.
  2. then do this Calculation as mentioned as above, and show all the results individually and save it.
Thanks very much for your help in advance
best regards
M.

Answers (1)

Voss
Voss on 31 Oct 2023
"I have nearly 100 csv Data sheet"
Assuming each sheet is in a separate file, then in order to read all the files, you need to move readmatrix to inside the loop.
Something like this:
filePattern = fullfile(myFolder, '*.csv');
S = dir(filePattern);
fullFileName = fullfile({S.folder}, {S.name});
for k = 1:numel(S)
fprintf(1, 'Now reading %s\n', fullFileName{k});
S(k).Data = readmatrix(fullFileName{k});
end
The structure array S contains the data from each file in its 'Data' field. So for example S(1).Data is the data from the 1st file, S(2).Data is the data from the 2nd file, and so on.
As for the calculation you want to do, do A1, A2, A3, A4, ..., B1, B2, B3, etc., represent columns in the files? Is the number of "A" columns the same as the number of "B" columns in each file, and are these numbers the same for all files?

Categories

Find more on File Operations in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!