concatenate structs and then plot a specified column

I have the following folder, named Dati_Finali:
folder.JPG
Each mat file contains a single variable named dati_finali, see the following figure, with different number of rows but equal number of columns:
cell.JPG
I would like to concatenate all the dati_finali cells vertically and then plot a specified column.
How can I do this? Thanks

답변 (1개)

Adam Danz
Adam Danz 2019년 10월 15일
편집: Adam Danz 2019년 10월 15일
Use dir() to list all files in the directory.
Loop through each file and load in the variable using load(filename,variables)
From within your loop you can vertically concatenate like so
C = [];
for i = 1:numberOfFiles
vars = load('filename','dati_finali')
C = [C;vars.dati_finali];
% ^ vertical concatenation
end
Then plot colun number 'n' by
plot(C(:,n))
If you have trouble putting any of this together, feel free to comment below and include the code.

댓글 수: 6

Can you plot the entire code starting from dir() please?
Adam Danz
Adam Danz 2019년 10월 15일
편집: Adam Danz 2019년 10월 15일
No.
But you can get it started and I'd be happy to help you do your work when you get stuck.
You only need to know 2 or 3 tools: dir, load, and plot. Loading the variables and the vertical concatenation is already shown in my answer.
You can quickly read through the documentation and look at examples.
I have followed your suggestion and I have tried to implement the code only for the mat files. However I would like to delete the first row of the matrices taht will be concatenated. How can I do this? This is my code
clc
clear all
file = dir();
C = [];
for i=3:4
vars = load(file(i).name,'dati_finali')
C = [C;vars.dati_finali];
end
file=dir() is ok if all of your files are in your current directory. Otherwise, you may want to include the directory path as the first input to dir().
To delete the first row of a matrix, 'M'
m(1,:) = [];
Do you want to delete the first row of every matrix? If so, you can just concatenate starting at row 2 as shown below.
for i=3:4
vars = load(file(i).name,'dati_finali')
C = [C;vars.dati_finali(2:end,:)];
% ^^^^^^^
end
I want to delete the first row of the uploaded matrices, except for the first one. If i use your code all first rows will be eliminated.
Moreover, plot of the 16th column is:
heights=C(:,16);
plot(cell2mat(heights(:,1)))
Adam Danz
Adam Danz 2019년 10월 15일
편집: Adam Danz 2019년 10월 16일
"I want to delete the first row of the uploaded matrices, except for the first one. "
That's what conditional statements are for.
for i=3:4
vars = load(file(i).name,'dati_finali')
if i==3 % <--3 because your i-loop starts with 3
Cnew = vars.dati_finali;
else
Cnew = vars.dati_finali(2:end,:);
end
C = [C; Cnew];
end
About your cell array, what's stored in that array? Are they all numbers or is there a mix of numbers and strings/characters or other stuff? The solution for plotting the n_th column will depend on what's in those arrays.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2019b

질문:

2019년 10월 15일

다시 열림:

2021년 8월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by