hello everybody,
I am new at using the program, i worked with maple before and i reason with its logic i am using the matlab to reduce some noise of some measures and every xls i export has the name 1.xls, 2.xls, 3.xls...like that until 75.xls and each one has a measure i made. After smoothing each measure, I need to calculate the surface and graph it with the A matrix. so I though of doing something like the code below.
A=[0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9,9.5,10,10.5,11,11.5,12,12.5,13,13.5,14,14.5,15,15.5,16,16.5,17,17.5,18,18.5,19,19.5,20,20.5,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35]
for i=1:75 %all the excels i have
[values(i)]=xlsread('i.xls','B10:B5010');
smooth(i)=sgolayfilt(values(i),1,447);
surf(i)=trapz(0.04,smooth(i));
Graph=[A(i) surf(i)]
end
plot(Graph)
xlswrite('essai',Graph)
I know that the program is going to search for the i.xls file and not for 1.xls, 2.xls, 3.xls...etc. I made it so everybody could understand better the logic i took. anyone knows a way to doing it? thanks

 采纳的回答

dpb
dpb 2016-10-5
A=[[0.5:0.5:21[ 22:35]; % use colon; recommend using the "Getting Started" tutorials
See the FAQ <process a sequence of files>. I'm particularly fond of the dir solution w/ wildcard for cases such as you show.
d=dir('*.xls'); % get the list of files...
for i=1:length(d) % and iterate over it...
values=xlsread(d(i).name,'B10:B5010'); % read the spreadsheet...
....
At this point, do the operations on the specific dataset and move on to the next. Doesn't look like you need to hold all in memory at once.

6 个评论

Thanks!!!! it was working until i tried with all the excels tougether... do you have any idea what is the problem?
if true
clear
OrdeGolay=1
framelen=501;
d=dir('*.xls');
X = 0:0.04:199.96;
A=[0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9,9.5,10,10.5,11,11.5,12,12.5,13,13.5,14,14.5,15,15.5,16,16.5,17,17.5,18,18.5,19,19.5,20,20.5,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35];
for i=1:length(d)
values=xlsread(d(i).name,'B10:B5010');
smooth=sgolayfilt(values,OrdeGolay,framelen);
surf(i)=trapz(X,values);
surfsmoth(i)=trapz(X,smooth);
relation(i)=surfsmoth(i)*100/surf(i);
if abs(relation(i)-100)>1
print(aumentar_el_OrdedeGolay)
end
plot(smooth);
end
essai=[A; surfsmoth]
% code
end
Well, not without at least a hint of what, specifically, "isn't working" means...do you get an error (if so, post the complete text in context) or just unexpected result or...?
i solved :D i was a problem with the files that i was using. a last question that maybe you know about the subject, i want to put the name of the file as the title in the plot smooth inside the loop so, every time i generate a plot it puts the name of the file in the title. i noticed that d is a structure but i couldnt find (atleast for the moment) a way to put it. in a logic way like this:
for i=1:length(d)
...
figure(i)
plot(smooth)
title([' ',d(i,1)])
end
i know i cant do it like this, i cant extract value so easy from a structure but i am struggling in find a way. i tried extractfield(d(i),name) without succes, and with getfield i succes to extract the name, but it gives me the name with the extension (in my case there are excel files) so e.g. 1.xls. is there any way to only take the name without the extension? thanks
[path,name,ext]=fileparts(d(i).name); % That was easy, wasn't it?
Granted, the lexicon for Matlab is overwhelming in its magnitude for the initiate. Hint: When you've got something like this you're looking for but don't know the name, try lookfor. In this case "filename" would seem a good thing to try to find...and indeed
>> lookfor filename
fileparts - Filename parts.
mexext - MEX filename extension for this platform, or all platforms.
mfilename - Name of currently executing M-file.
movie2avi - (MOV,FILENAME) Create AVI movie from MATLAB movie
>>
and voila!!! in this case you indeed did find it exactly in a pretty short list. Doesn't always work so well, but using it and help plus just looking thru the documentation contents will pay large rewards quickly.
ADDENDUM Got wrapped up in the tutorial, sorry! :) So, the solution to the question would be something like--
[~,fname]=fileparts(d(i).name); % save the root name
title([Smoothed response file: ' fname])
Note since the root name is the second output argument must save it into a variable; there's unfortunately no Matlab syntax to use anything other than the first return variable from a function reference in an expression.
thanks! yes i am using the help(and the question already asked from other people), now i am going to give a try to the look for! thanks. i found that function but sometimes i dont understand how to use them,for example for this function i didnt catch it yet.... i find a little difficult the logic sometimes in matlab (it is difficult to change between softwares ahahaha) but i begin understanding. i will keep working in it and reading the help :) thanks for taking the time!
Have you used the text-based help command from the command line or just the documentation for individual functions or the general documentation. While not pretty, what it does is let you "drill down" from the various subdirectories by which Matlab organizes functions, giving you the list of what those are and then if there's a category of interest, using that category/directory as the argument gives an overview list of all functions and the one-line help. That can be invaluable aid in finding the function(s) related to what it is you're looking for at the time in a quicker way than the other documentation. You then use that doc to find out the details.
That's a "brute force" technique but is well worth doing a few times just to gain some familiarity with the contents...recollecting you've seen something like that in the past is helpful even if can't remember a tenth of it specifically, the generalities are beneficial.
OTOH, lookfor is a search tool for a keyword that you think could be related to the topic you're searching for the tools available for...
>> help lookfor
lookfor Search all M-files for keyword.
lookfor XYZ looks for the string XYZ in the first comment line
(the H1 line) of the HELP text in all M-files found on MATLABPATH
(including private directories). For all files in which a
match occurs, lookfor displays the H1 line.
...
In summary, WHAT lists the functions in a given directory,
WHICH finds the directory containing a given function or file, and
lookfor finds all functions in all directories that might have
something to do with a given key word.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by