Question

I have two questions:

  1. 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.

  2. 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.

Était-ce utile?

La solution

Regarding bullet no. 1:

I have open figures in matlab and I want to print them all to a file, but also to my printer.

You have the print command for this purpose. Just specify the handle of the figure, for example:

print(h)                      %// Send figure with handle h to printer
print(h, '-djpeg', filename)  %// Save figure with handle h to a JPEG file

Regarding bullet no. 2:

... can I simplify it with a loop function?

Yes you can. You can do something like this:

N = 4; %// Number of Excel files
C = cells(2 * N, 1);
for k = 1:2 * N
    filename = sprintf('E:\MATLAB\Indi%d_Output_SFTW1_6min_100_020812.xls', k);
    C{k} = xlsread(filename, sprintf('Indi%d_Sim_T_SFTW12', k));
    C{k + 1} = xlsread(filename, sprintf('Indi%d_Sim_T_SFTW12', k));
end

Now all the data is stored in cell array C.


Explanation

I'm planning to store the output of xlsread in a cell array. You have N files and you're reading data from two worksheets per file, and therefore the cell array C is initialized with 2 * N cells.

The for loop iterates over each file, and constructs the filename, using the command sprintf and the iteration counter k:

filename = sprintf('E:\MATLAB\Indi%d_Output_SFTW1_6min_100_020812.xls', k);

The result here is stored in the variable filename, and it is passed to the xlsread command. The sheet names are also constructed using sprintf, and passed directly to the xlsread without 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, write C{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 xlsread returns data of the same length, a simple matrix couldn't be used to store the contents of all files.

Hope this helps!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top