how can expand a m file?

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 commentaires

Mahan Soltani
Mahan Soltani le 17 Déc 2012
no, same problem
Jan
Jan le 17 Déc 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.

Connectez-vous pour commenter.

 Réponse acceptée

Walter Roberson
Walter Roberson le 17 Déc 2012
Modifié(e) : Walter Roberson le 17 Déc 2012

0 votes

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 commentaires

Mahan Soltani
Mahan Soltani le 17 Déc 2012
thanks walter, i copy your code in a new script and run it, but this error appear : >> Untitled Error using xlsread (line 129) XLSREAD unable to open file 'subject_1.xls'. File 'C:\Users\RoYaL\Desktop\Excel\subject_1.xls' not found.
Error in Untitled (line 10) [numdata,txtdata] = xlsread(xlsfile);
Mahan Soltani
Mahan Soltani le 17 Déc 2012
sorry, i have 10 xlsx file! not xls when i change xls to xlsx and run scipt, this error seen : Undefined variable numdat.
Error in Untitled2 (line 12) all_saj{K} = numdat(:,2);
>>
Walter Roberson
Walter Roberson le 17 Déc 2012
I have corrected the code, above.
Mahan Soltani
Mahan Soltani le 17 Déc 2012
thanks,walter? please download this folder and locate it in your desktop, then start MATLAB and test your answer,finally create a m file without error , i have MATLAB r2012a thanks a lot
Walter Roberson
Walter Roberson le 17 Déc 2012
Re-corrected.
You really should be able to handle these minor things (such as reversing two arguments) by yourself. Learn to read the documentation.
Walter Roberson
Walter Roberson le 18 Déc 2012
Modifié(e) : Walter Roberson le 18 Déc 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 le 18 Déc 2012
Change "potfile" to "plotfile" yourself. And comment out the call to legend() if it is giving you problems.

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 17 Déc 2012

0 votes

In order to use it for more excel files, you need to remove the "clear all"

4 commentaires

Mahan Soltani
Mahan Soltani le 17 Déc 2012
no,i have 10 exel files in my desctop and want to shoe all exel files in matlab with together,exaple: showe all plot in one window to compare them how can change this m file to show 10 subject ? now this m file just show data of one subject(sibject_1),i need 10 subject to compare them thanks
Walter Roberson
Walter Roberson le 17 Déc 2012
I assure you, you have no chance of getting that program to work for multiple files unless you remove the "clear all".
* 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 le 17 Déc 2012
Modifié(e) : Walter Roberson le 17 Déc 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Import and Analysis dans Centre d'aide et File Exchange

Produits

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by