Problem calling multiple files through for-loop
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Jose Rego Terol
le 1 Sep 2019
Modifié(e) : Stephen23
le 8 Sep 2019
This is the for-loop function, named spike_analysis_wrapper:
function spike_analysis_wrapper()
filepairs = {
'spike1.mat', 'footsignal_spike1.mat';
'spike2.mat', 'footsignal_spike2.mat'};
% Loop through each row of filepairs
for i = 1:size(filepairs)
% Send the 2 files in row "i" into the analysis
spike_analysis(filepairs{i,1},filepairs{i,2});
end
end
And this is the main function, named spike_analysis:
function spike_analysis (file1,file2)
load(file1)
%Plot the spike with a corrected baseline
A=spike1(:,1);
B=spike1(:,2);
B1=B-(min(B)); %Baseline is 0 if the minimum value is diferent than 0.
figure(1)
plot(A,B1)
ylabel('Intensity (pA)')
xlabel('Time(s)')
title('1st spike')
% The paramenters of the foot signal
load(file2)
A_f=footsignal_spike1(:,1);
B_f=footsignal_spike1(:,2);
figure (2)
plot(A_f,B_f)
ylabel('Intensity (pA)')
xlabel('Time(s)')
title('1st foot signal')
end
The for-loop execute the first set of files (spike1 & footsignal_spike1) but I got this error when the loop tries to execute the second set of files (spike2 & footsignal_spike2).
Undefined function or variable 'spike1'.
Error in spike_analysis (line 5)
A=spike1(:,1);
Error in spike_analysis_wrapper (line 8)
spike_analysis(filepairs{i,1},filepairs{i,2});
According to the error, the problem lies on the main function (spike_analysis) because of the variable spike1.
How can I fix this problem?
Thanks in advance!
1 commentaire
Réponse acceptée
Stephen23
le 1 Sep 2019
Modifié(e) : Stephen23
le 3 Sep 2019
The problem is caused by badly designed .mat files, where every .mat file contains variables with different names. Your code assumes that the second files contains variables of the same name/s as the first file, but clearly this is not the case.Parsing multiple .mat files in a loop is much simpler when every .mat file contains exactly the same variable names.
S = load(file1);
C = struct2cell(S);
A = C{1}(:,1);
B = C{1}(:,2);
And the same for file2.
If your .mat files contain multiple variables per file, then you can load into a structure and then use dynamic fieldnames:
10 commentaires
Stephen23
le 8 Sep 2019
Modifié(e) : Stephen23
le 8 Sep 2019
"How can I save a table for all files loaded in the loop's script that contains all the outputs of the function for each file loaded in one .xlsx file?"
Using writetable or xlswrite, both of which let you specify the destination worksheet and/or range to save the data to (thus easily avoiding overwriting data).
"Obviusly, the file was overwriting everytime by the loop."
You forgot to write the new data to different sheets and/or ranges, to ensure that the data was not overwritten.
"...but the table only contains the first output of the function of the last file loaded."
In fact what your code shows is that you did not call your function with any output arguments at all (something that has been thoroughly discussed in previous comments). The table Table_parameters_spike is being displayed in the command window, and you have not defined it as an output of your function. You appear to be confusing some data being displayed in the command window with the output arguments of your function (none of which appear to be tables).
"Actually, the value of the function in the loop is only the first output of the function."
Because you did not allocate the function outputs to any variables, MATLAB allocates the first ouput argument to the default ans:
"It is abnormal because the function has 5 output and the loop should give all the outputs, right?"
Nope, not abnormal at all... in fact because you did not allocate any of the outputs to variables, I would not expect "all the outputs", I would expect only the first one allocated to the default ans.
Very basic MATLAB concepts, such as how to call functions with multiple output arguments, are explained in the introductory tutorials:
You need to decide if you want to:
- return the table from the function (in which case it needs to be an output argument AND you need to call the function with an appropriate number of output arguments AND you need to decide if you also save the table inside the loop (see point 2) or somehow join/concatenate the data into one table and save it after the loop)
- save the table inside the loop (in which case you need to decide what destination worksheet/range to use to prevent data from being overwritten).
So what would you prefer?
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Variables dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!