I have two questions:
-
I have like twelve open figures in MATLAB (already generated by another function, figure 1 to 12) and I want to print them all to a file, but also to my printer. How can I write the code for that? I already read about getting handles of the figures but I didn’t really get the meaning of it and how to print them after that.
-
I wrote a script to import data from many Excel files to MATLAB. The excel files are named so that the only difference is in the the initial part which includes the name/code of the individual person (for whom the information is).
My script looks like this:
Indi1_Sim_T_SFTW1= xlsread('E:\MATLAB\Indi1_Output_SFTW1_6min_100_020812.xls','Indi1_Sim_T_SFTW12')
Indi1_Sim_V_SFTW1= xlsread('E:\MATLAB\Indi1_Output_SFTW1_6min_100_020812.xls','Indi1_Sim_V_SFTW12')
Indi2_Sim_T_SFTW1= xlsread('E:\MATLAB\Indi2_Output_SFTW1_6min_100_020812.xls','Indi2_Sim_T_SFTW12')
Indi2_Sim_V_SFTW1= xlsread('E:\MATLAB\Indi2_Output_SFTW1_6min_100_020812.xls','Indi2_Sim_V_SFTW12')
Indi3_Sim_T_SFTW1= xlsread('E:\MATLAB\Indi3_Output_SFTW1_6min_100_020812.xls','Indi3_Sim_T_SFTW12')
Indi3_Sim_V_SFTW1= xlsread('E:\MATLAB\Indi3_Output_SFTW1_6min_100_020812.xls','Indi3_Sim_V_SFTW12')
Indi4_Sim_T_SFTW1= xlsread('E:\MATLAB\Indi4_Output_SFTW1_6min_100_020812.xls','Indi4_Sim_T_SFTW12')
Indi4_Sim_V_SFTW1= xlsread('E:\MATLAB\Indi4_Output_SFTW1_6min_100_020812.xls','Indi4_Sim_V_SFTW12')
And it goes on for many other individuals.
As you can see, I am repeating the code for each individual. Can I simplify it with a loop function?
P.S: all imported data are numbers.
Regarding bullet no. 1:
You have the
printcommand for this purpose. Just specify the handle of the figure, for example:Regarding bullet no. 2:
Yes you can. You can do something like this:
Now all the data is stored in cell array
C.Explanation
I’m planning to store the output of
xlsreadin a cell array. You haveNfiles and you’re reading data from two worksheets per file, and therefore the cell arrayCis initialized with2 * Ncells.The
forloop iterates over each file, and constructs the filename, using the commandsprintfand the iteration counterk:The result here is stored in the variable
filename, and it is passed to thexlsreadcommand. The sheet names are also constructed usingsprintf, and passed directly to thexlsreadwithout being stored in a variable first.Q: Why storing the filename in a variable but not the sheetnames?
A: I’ve decided to store the filename in a variable just because I don’t want to construct it twice, whereas the sheet names are constructed only once, so storing each of them in a variable bears no significance.
After the loop, the entire data is stored in cell array
C. To access the i-th element (cell) in C, use curly braces ({}). For example, to access the second cell, writeC{2}.Q: Why cell array?
A: In a cell array each cell can contain data of a different type and length, for instance {2, ‘hello’} is a cell array that contains two cells: one contains the number 2 and the other contains the string “hello”. Since you cannot guarantee that
xlsreadreturns data of the same length, a simple matrix couldn’t be used to store the contents of all files.Hope this helps!