How to re-produce an existing variable in the script without using it?

조회 수: 4 (최근 30일)
TingTing
TingTing 2015년 11월 19일
편집: Stephen23 2015년 11월 19일
Hi, this sounds silly... I have a variable in my workspace... say it is called A of size (200,1). Each element of it is just a number, e.g. A=[3;3;444;555;...]... Now I would like to define the exact same A in the script without using the A in workspace... Of course, I can do it manually A=[3;3;444;555;...], but that takes forever. Is there a smart way that I can define A in script without having to have A in the workspace? Thanks!
  댓글 수: 1
Stephen23
Stephen23 2015년 11월 19일
편집: Stephen23 2015년 11월 19일
You write in a comment below that "the problem is the script/model will be used later without any variable in the workspace... So A will no longer be there", but this only seems to be relevant when MATLAB is restarted, otherwise there are multiple ways pass variables between workspaces, or to make them persistent, or pass them as arguments...
Can you please explain the exact sequence that makes it impossible to pass the variable using standard means? Perhaps there is some easy way that we can recommend, if you tell us exact how it occurs that the variable A is not accessible.
In particular can you tell us what you mean by "the workspace", as MATLAB has multiple workspaces: the base workspace and for every function. Of course it is also easy to pass arguments between workspaces, so your difficulty is not clear.

댓글을 달려면 로그인하십시오.

답변 (4개)

Stephen23
Stephen23 2015년 11월 19일
편집: Stephen23 2015년 11월 19일
It seems that you are looking for functionality like this FEX submission uneval:
This submission takes a variable and prints a string that when evaluated reproduces the same variable as the function was given. I do not think that the author has updated it to include tables, however you could write to them and request this, or make the required changes yourself.

Thorsten
Thorsten 2015년 11월 19일
You can use dlmwrite the write A to a file, open the file in an editor and copy and paste the contents to your script and add the A = [ ...]; syntax.
  댓글 수: 1
TingTing
TingTing 2015년 11월 19일
Thanks a lot! This would have worked if my A is a numerical variable... However, my A is a table with both row name and column names... Any remedy?

댓글을 달려면 로그인하십시오.


Ilham Hardy
Ilham Hardy 2015년 11월 19일
What do you mean by slow and not using the variable in workspace?
B = A; ?
  댓글 수: 2
TingTing
TingTing 2015년 11월 19일
No. no... the problem is the script/model will be used later without any variable in the workspace... So A will no longer be there later...
Ilham Hardy
Ilham Hardy 2015년 11월 19일
Are you talking about script (in Matlab) or model (in Simulink)?
If the mysterious variable will be used in the model (Simulink) then you could use the Model Propertise; Callback

댓글을 달려면 로그인하십시오.


Guillaume
Guillaume 2015년 11월 19일
Probably the easiest way is to save the variable to a mat file. The first thing you do in your script is load the variable from the mat file.
  댓글 수: 1
TingTing
TingTing 2015년 11월 19일
yes, but i am not allowed to use mat file as well... All I can do it to put everything into the script... so the A has to be defined in the script as if hard-coded.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by