Dear community I am doing something wrong with my script. I am trying to automize my script. The load function is given me a error. I dont know exaclty what I am doing wrong. My dataset is as follow from data1 till data40. From each data I want to get a plot and name the plots for data1 --> data1. Can someone please help me out.
As shown below
matfiles= dir ('data*')
N = length(matfiles) ;
for i = 1:N
load(matfiles(i).) ; % I want to load for example data 1 of the dataset and then further naming the variables of each dataset. giving the
frequency = data(:,1);
zabsolute = data(:,2);
degree = data(:,3);
x= frequency * 1000; %% frequency is given in KHz multiply with 1000
y1 = zabsolute .* cosd(degree); %% formula to calculate the imaginair component of impedance
y2 = zabsolute .* sind(degree); %% formula to calculate the real component of impedance
figure(i)
subplot(2,1,1);
plot(x,y1),
title ( ' Resistance ' );
xlabel ( ' Frequency (Hz)');
ylabel ( ' Re Z');
subplot (2,1,2);
plot (x,y2);
title ( ' Reactance' );
xlabel ( ' Frequency (Hz)');
ylabel ( ' Img Z')
end

 채택된 답변

Stephen23
Stephen23 2021년 3월 10일
편집: Stephen23 2021년 3월 10일

1 개 추천

load(matfiles(i).)
% ^^ You forgot to write the fieldname.
Note that you should specify the file extension and for more reliable code always load into an output variable:
S = dir('data*.mat');
for k = 1:numel(S)
D = load(S(k).name);
frequency = D.data(:,1);
zabsolute = D.data(:,2);
degree = D.data(:,3);
..
end

댓글 수: 6

Bayram Akpinar
Bayram Akpinar 2021년 3월 10일
Thank you Stephen.
If I add this script to mine then I got an error of 'Dot indexing is not supported for variables of this type.'
And if I look into my workspace only 'data1' is included in the workspace. I do not know what to do.
Stephen23
Stephen23 2021년 3월 10일
@Bayram Akpinar: please upload a sample file by clicking on the paperclip button.
Bayram Akpinar
Bayram Akpinar 2021년 3월 10일
The file that I want to upload does not contain any file format. I uploaded a zip file that contains 2 files in it. I will be more clear for you when you are working on these files what I am struggling with. Thank you in advance Stephen.
Stephen23
Stephen23 2021년 3월 11일
편집: Stephen23 2021년 3월 12일
"The file that I want to upload does not contain any file format. "
That the filenames do not have file extensions is irrelevant to what file format they have. Actually both of the files that you uploaded are text files, very neatly formatted with tab-separated values and containing only numeric values.
Import them using readmatrix, something like this:
S = dir('data*'); % no dot, no extension!
S = S(~[S.isdir]);
for k = 1:numel(S)
M = readmatrix(S(k).name);
frequency = M(:,1);
zabsolute = M(:,2);
degree = M(:,3);
..
end
Bayram Akpinar
Bayram Akpinar 2021년 3월 11일
Thanks for the help Stephen. I will look forward into it. I think the dot has to be removed in the first line or else it does not run the script. I will try it out and let you know if it succeds me to get figures out of this script. Agian much thanks to you Stephen.
Bayram Akpinar
Bayram Akpinar 2021년 3월 11일
편집: Bayram Akpinar 2021년 3월 11일
It helped me a lot thanks to you. I get all the figures at once now.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

태그

질문:

2021년 3월 10일

편집:

2021년 3월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by