How do I save matrix from workspace to a function in the editor in MATLAB?

36 views (last 30 days)
Hi guys,
I've write in the workspace of matlab matrix called a and it's really too big 300x300 ;
I've about three days to fille it with specific values that I want to fill it.
my matrix is an interger matrix - it means it have integer values.
After I filled it, I want to use that matrix in a function that Im building in the editor of matlab (not the workspace ..I mean the editor above that I write there a function)
the function called
function doSomething=doSomething(Data)
%here I want to use the matrix a and to implement in it some manipulation
end
the problem that the matrix a is in my workspace and saved in the workspace variables, so if I want to write it again in the function
doSomething it takes me more time because it's 300x300
So what should I do for writing quickly the matrix a in the function doSomething? I mean by writing the matrix a in the function
doSomething in order to be able to use the matrix a in that function (the workspace variables isn't saved for function doSomething and I cant use the workspace variables in the function doSomething because it's not allowed in matlab)
could anyone help me out how I can save/use/write the workspace variable -in my case it's matrix a - and to be able to use it in my function that Im building in the editor of matlab without rewritting the matrix a itself in the function itself ? otherwise it takes really much time to re-write the a matrix 300x300 again in the function itself...is there anyway to save/use the a matrix in my function of matlab editor without re-writing it? all the point that I dont want to re-write the matrix a in my function editor of matlab in order to be able to use it through the editor/function itself.. the workspace variables aren't known for the matlab editor (matlab editor is where we write a function).
Regards

Answers (3)

Ameer Hamza
Ameer Hamza on 14 Nov 2020
Edited: Ameer Hamza on 15 Nov 2020
Passing variable as input to the function is probably the most efficient way. Write your function like this
function output=doSomething(Data, a)
%here I want to use the matrix a and to implement in it some manipulation
end
and then pass the variable 'a' from base workspace to the function like this
doSomething(data, a)
  4 Comments
Ameer Hamza
Ameer Hamza on 15 Nov 2020
@Image Analyst, thanks for point out.
@Jimmy, If you just want to save the content of the matrix so that you can use it later, then you can use save() function: https://www.mathworks.com/help/matlab/ref/save.html to save it in a .mat file, and then use load() function when you open MATLAB next time, or just double click the .mat file inside MATLAB. The variable will be loaded in MATLAB.

Sign in to comment.


Image Analyst
Image Analyst on 14 Nov 2020
  1. 300x300 is really a microscopic array. Not large at all. Not even a small fraction of the size of a typical digital image from a camera. So what is taking so long? If you were doing something simple, it should take microseconds. Exactly what kind of time consuming operations are you doing such that it takes 3 days?
  2. And what is 300x300? Data or "a"? Or both?
  3. What (Data or a) is passed into doSomething() and what variables does doSomething() return?
  4. How many times will you be calling doSomething()? 90000?
  5. The output variable should not have the same name as the function.
  2 Comments
Jimmy cho
Jimmy cho on 14 Nov 2020
Hi
thanks for your reply.
the "a" matrix is an integer matrix which means its values/data are integers (1 2 3 ..etc).
what's bothering me that I want to use the matrix "a" in function doSomething and the matrix "a" is written in the workspace of matlab and not in the editor of matlab where I write my function.
I can re-write again the matrix "a" inside the function doSomething but it would takes much time to write it again with specified values that I already written them in the workspace where I initialized my matrex.
so what Im asking is that if there's a way to save the variable that I write in my workspace (in my case the variable is matrix) and to use it in the function doSomething, I want to do that in order if I re-opened matlab again ..I go to that function doSomething and the matrix "a" isn't cleared out in opposite to the workspace's variables that are deleted when I re-open matlab again.
to sum up, I made a mistake that I initialized matrix "a" in the workspace and not on the editor of matlab where the function built ..
the matrix "a" is constant , I mean every time I call the function doSomething it starts with the values that I already initialized it.
thanks alot
Image Analyst
Image Analyst on 14 Nov 2020
Not sure why you didn't answer any of my questions. But I'm going to add to them now.
  1. How did the (badly-named) a get into the workspace? Did you have a script define it or read it in from a file? It didn't just magically appear there so what put it there?
  2. What workspace is it in? The "base" workspace?
  3. When you call doSomething, where are you calling it from? A script? A function? Or the command line in the command window? Yes, this matters.
  4. Why don't you just call it like results = doSomething(Data, a) ???

Sign in to comment.


Walter Roberson
Walter Roberson on 14 Nov 2020
If you really need to (which you likely do not need in your case), you can use mat2str() to format numeric scalar or numeric vector or 2D numeric array into a character vector that would reproduce the content (but not data-type) of the array.
  5 Comments
Walter Roberson
Walter Roberson on 14 Nov 2020
save('a.mat', 'a');
And inside the function doSomething
adata = load('a.mat', 'a');
a = data.a;
Or once go to the workspace where you have a and do
['a = ', class(a), '(', mat2str(a), ');']
and copy the text and paste it inside of function doSomething .m so it is right there in the code.
Or if a is in the base workspace, then inside doSomething do
a = evalin('base', 'a');

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!