how can expand a m file?

2 views (last 30 days)
Mahan Soltani
Mahan Soltani on 17 Dec 2012
this is a m file for one xml file. how can i change it for show(plot,mat file and etc)more exel files? m file is :
clc
clear all
close all
[numdata,txtdata]=xlsread(['subject_1.xls']);
Ankle_saj_1=numdata(:,2);
max_pf_1=min(Ankle_saj_1);
max_df_1=max(Ankle_saj_1);
save('Ankle_saj_var_1','max_pf_1','max_df_1')
plot(Ankle_saj_1)
saveas(gcf,'Ankle_saj_1.fig')
[Merged information from duplicate question]
i have this m file:
[numdata,txtdata]=xlsread(['subject_1.xls']);
Ankle_saj_1=numdata(:,2);
max_pf_1=min(Ankle_saj_1);
max_df_1=max(Ankle_saj_1);
save('Ankle_saj_var_1','max_pf_1','max_df_1')
plot(Ankle_saj_1)
saveas(gcf,'Ankle_saj_1.fig')
-this m file collect data (Ankle_saj_1 , max_pf_1 , max_df_1 and plot(Ankle_saj_1)) from subject_1.xls and show them and finally save it. now, i have 10 xls file (subject_1 subject_2 ,...,subject_10). i need this data for all xls file to compare them.(for example: show 10 plot in one window to see all). Joes said that i need structure and loop to show all of them.i'm amautor and dont know about this works. please help me,this is for CP persons.thanks. download m file
  3 Comments
Mahan Soltani
Mahan Soltani on 17 Dec 2012
no, same problem
Jan
Jan on 17 Dec 2012
@Mohammad: If this is the same problem, please do not post another question. Such "double posting" is inefficient for you and for all readers, because it is additional work to keep the overview over both threads. Therefore double-posts are delete usually.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 17 Dec 2012
Edited: Walter Roberson on 17 Dec 2012
The code could be simplified a fair bit if it was not necessary to save the information to individual .mat files using different variable names for each .mat file, and also if it was not necessary to save the individual plots.
for K = 1 : 10
xlsfile = sprintf('subject_%d.xlsx', K);
matfile = sprintf('results_%d.mat', K);
saj_field = sprintf('Ankle_saj_%d', K);
pf_field = sprintf('max_pf_%d', K);
df_field = sprintf('max_df_%d', K);
plotfile = [saj_field '.fig'];
[numdata,txtdata] = xlsread(xlsfile);
all_saj{K} = numdata(:,2);
yourStruct.(saj_field) = all_saj{K};
yourStruct.(pf_field) = min( all_saj{K} );
yourStruct.(df_field) = max( all_saj{K} );
save(matfile, '-struct', 'yourStruct');
plot(all_saj{K});
saveas(gcf, potfile);
end
ph(1) = plot(all_saj{1});
hold all
for K = 2 : length(all_saj)
ph(2) = plot(all_saj{K});
end
legend(ph, cellstr( num2str( (1:length(all_saj)).', 'Subject #%d' )));
  7 Comments
Walter Roberson
Walter Roberson on 18 Dec 2012
Edited: Walter Roberson on 18 Dec 2012
for K = 1 : 10
xlsfile = sprintf('subject_%d.xlsx', K);
matfile = sprintf('results_%d.mat', K);
saj_field = sprintf('Ankle_saj_%d', K);
pf_field = sprintf('max_pf_%d', K);
df_field = sprintf('max_df_%d', K);
plotfile = [saj_field '.fig'];
[numdata,txtdata] = xlsread(xlsfile);
all_saj{K} = numdata(:,2);
yourStruct.(saj_field) = all_saj{K};
yourStruct.(pf_field) = min( all_saj{K} );
yourStruct.(df_field) = max( all_saj{K} );
save(matfile, '-struct', 'yourStruct');
plot(all_saj{K});
saveas(gcf, plotfile);
end
ph(1) = plot(all_saj{1});
hold all
for K = 2 : length(all_saj)
ph(2) = plot(all_saj{K});
end
legend(ph, cellstr( num2str( (1:length(all_saj)).', 'Subject #%d' )));
Walter Roberson
Walter Roberson on 18 Dec 2012
Change "potfile" to "plotfile" yourself. And comment out the call to legend() if it is giving you problems.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 17 Dec 2012
In order to use it for more excel files, you need to remove the "clear all"
  4 Comments
Walter Roberson
Walter Roberson on 17 Dec 2012
* Clear All Variables And Functions
clear all
This is not for erasing the previous command. "clear all" is like driving a car down a highway and suddenly all of the gasoline is gone and suddenly all of the air is gone from the tires and suddenly the car is turned off and the keys are not even in the ignition anymore.
Walter Roberson
Walter Roberson on 17 Dec 2012
Edited: Walter Roberson on 17 Dec 2012
Perhaps you were composing that comment while I was posting the answer...
The problem is not simple because you want the saved variable names to match the subject number.

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!