tags:

views:

35

answers:

2

Suppose we have expressions like u1=1/24*h*sin(3*t)-1/24*h*sin(3*t)*k+1/24*h*sin(t)-1/24*h*k*sin(t). After saving it in a .mat file how can we load it into matlab.I am new user of matlab. We are using symbolic toolbox of matlab version 5.3.After using char command we can save our expression in mat file or in other files too.For example, fid=fopen('out.mat','r'); syms t k class(t); u0=sin(t)+k*cos(t)^2; u0=char(u0); fprintf(fid,'u0=%s',u0); fclose(fid);

This will save u0 in the file out.mat or whatever the name chosen, in my best of knowledge.If you have any better option,then please tell me. So,now my question is how to load it or how will we use this expression in other files. Sorry for not giving full information about the problem.I have tried the command load filename,but it gives "file can not be open".I don't know.Why?

A: 

Your question is a bit confused and a bit confusing. How have you saved the expression in a mat file ? The usual answer to how to load from a mat file is simply to execute the command load 'matfile.mat' but that depends on having saved the mat file correctly in the first place.

Are you using the Matlab Symbolic Toolbox (or whatever it is called) ? Or are you just trying to save a Matlab expression ? If the latter, then an m-file is probably a better approach than a mat file.

Please clarify your question.

High Performance Mark
+1  A: 

The proper way to save and load variables to a .mat file is to use the functions SAVE and LOAD. For example:

syms t k                 %# Declare your symbolic variables
u0 = sin(t)+k*cos(t)^2;  %# Make your symbolic equation
save out.mat u0          %# Save u0 to out.mat

Then you can load the variable u0 by doing the following:

load out.mat             %# Load u0 into your workspace

You can also use the functional forms of SAVE and LOAD if your file name is stored as a string:

save('out.mat','u0');  %# Save u0 to out.mat
load('out.mat');       %# Load u0 into your workspace
gnovice

related questions