开发者

How to convert MATLAB output into an array?

I've made an M-file that outputs data into my MATLAB command window in the form below (I've deleted the extra spaces). Is there an easy way to convert this output into an array, without having to type all the data into the array editor as I'm currently doing? (Or even run it straight from the开发者_JAVA百科 M-file into an array?)

T =    0.3000
price =   24.8020

T =    0.4000
price =   28.3453

T =    0.5000
price =   31.3934

T =    0.6000
price =   34.0880


Organize your data into arrays. For example:

T=0.3:0.1:0.6;
Price=yourfunction(T);

Then if you want a price vs T graph,

plot(T,Price)

If you've got a large amount of data, try to avoid for loops, as they're slower than vectorized code.


At some point in your M-file you are printing each line of data to the command window, presumably using DISP or FPRINTF. You can replace that line with the following:

data = [data; T price];

Where T and price are the variables holding your data. Every time you call the above line (say, in a loop) it will append your data as a new row to the variable data. At some point at the beginning of your M-file, you would therefore have to add the following initialization:

data = [];  %# An empty array

Appending values to an array like this can sometimes be inefficient, so if you already know ahead of time how many rows of data you will collect you can instead preallocate data with a given size. For example, if you know you will have 4 pairs of values for T and price, you can initialize data in the following way:

data = zeros(4,2);  %# A 4-by-2 array of zeroes

Then, when you add data to the array you would instead do the following:

data(i,:) = [T price];  %# Fill row i with data

Another issue to consider is whether your M-file is a script or a function. A function M-file has a line like function output = file_name(input) at the top, whereas a script M-file does not. Running a script M-file is equivalent to typing the entire contents of the file directly into the MATLAB command window, so all of the variables created in the M-file are available in the workspace.

If you are using a function M-file, all variables created are local to the function, and any you want to use in the workspace will have to be passed as outputs from the function. For example, the top line of your function M-file could look like:

function data = your_file

where your_file is the name of the M-file and data is a variable being returned. When you call this function from the workspace you would then have to capture the output in a variable:

outputData = your_file();

Now you have the contents of the variable data from your_file stored as a new variable outputData in the workspace.


Why do you print out data instead of collecting it in an array?

M = [];
for ...
   M(end+1, :) = [T, price];
end;

or, more idiomatically,

M = 0.3:0.1:0.6; % or whatever your T values should be
M = [M' (M'.^2)] % replace the .^2 by your price function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜