views:

312

answers:

3

Hi there I hope you can help me. I've made an M-file that outputs data into my Matlab command window in the form below (I've deleted the extra spaces). My question is, 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 M-file into an array?) Many thanks.

T =    0.3000
price =   24.8020

T =    0.4000
price =   28.3453

T =    0.5000
price =   31.3934

T =    0.6000
price =   34.0880
A: 

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
Christopher Creutzig
Thanks for your reply.In answer to your first question, because I have no idea how to do it any other way. I think your first bit of code may be what I am after, but I'm afraid I can't get either of those bits of code to work. Do I need to include something else in the first one? It says 'Unbalanced or unexpected parenthesis or bracket' when clearly they are not.As for your second, it won't allow me to replace .^2 by my function, as my function is a compicated m file rather than just ^2.
+1  A: 

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.

Doresoom
MATLAB is an array focused language. The T and Price variables will be stored as arrays, and you can apply mathematical functions to the entire array at once. The code I posted above should be all you need, with the addition of your function. (Am I assuming correctly that T is the input, and it returns price? Or are both inputs with a separate output?)
Doresoom
Your assumption is correct. I think this major part of my problem is sorted now. Thanks :)
+1  A: 

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.

gnovice
Thanks very much. With some help I was able to get your method to work as I wished. :-)I'm pretty sure my M-file is a function and not a script as it has the form you suggest.