MATLAB Answers

Open multiple .csv files, process and save them in a structure

7 views (last 30 days)
JoKi
JoKi on 8 Jul 2020
Commented: JoKi on 8 Jul 2020
Hello guys,
im very new to Matlab and have to code my own program to analyse some data. I need to open multiple .csv files (done that with uigetfile), filter them for the needed data (which is in column 7 and starts at row 31, i have put in a photo of how my data looks). My problem is that i cant save the files the way i want after i have opened them. I need to filter and plot the extracted data afterwards. The code i have so far works but is only saving the first file.
My Plan was to save each data with the loop in a structure so that i can access them later. If there is another way to save the data, im fine with it too, doesnt have to be with a structure i just thought this could work.
[filename,pathname] = uigetfile('*.csv;','MultiSelect',"on");
for i = 1,length(filename)
filen = filename(1,i);
filepath=fullfile(pathname, filename);
fid = fopen (filepath{i});
%[file,path]=uigetfile('*.csv');
A = textscan(fid, '%s%s%s%s%s%s%s%s%s%s%s%s%s','Delimiter',';');
%Umwandeln von Cell --> String
B=string([A{:}]);
%',' durch '.' ersetzen
B=replace(B,',', '.'); % ',' durch '.' ersetzen
% Aufteilen in Header und Messdaten
Daten.Header=B(1:30,:);
Daten.Messdaten=str2double(B(31:end,7));
end
I am using the Matlab AppDesigner.
Since this is my first question here i hope this is enough information.

  0 Comments

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 8 Jul 2020
You need to fix this line otherwise your loop will only iterate once:
for i = 1:length(filename)
% ^ this must be a colon!
You also need to use indexing to allocate to the output array on each loop iteration, e.g.:
Daten(i).Header = ...
Daten(i).Messdaten = ...
Read more about structure arrays:

  1 Comment

JoKi
JoKi on 8 Jul 2020
Ahh thanks, so far it works now.
I have tried to name it Daten.Header(i) before, didnt know i had to put the (i) there.
Thanks alot!

Sign in to comment.

More Answers (0)


Translated by