Hello I want to call special matrix that I created before via function only by name of matrix. for example I create
z1=[12 15 16;31 65 79;20 22 34]
I write this function:
function[f]= ave1(z1,z2)
z1=input('input z1 matrix: ')
[n1,n2]=size(z1);
n3=n1*n2
[n4,n5]=size(z2);
z2=input('input z2 matrix: ');
n6=n4*n5
B=reshape(z1,n3,1);
C=reshape(z2,n6n1);
f=[A(:);B(:)]
end
but when I run it in Matlab it can't khnow that. How can introduce or call my matrix in function only by name of matrix? Thank you

 採用された回答

the cyclist
the cyclist 2014 年 6 月 23 日
編集済み: the cyclist 2014 年 6 月 23 日

0 投票

The reason you get this error is that z1 doesn't exist inside the function at the time of the input command.
The only way I can think of to do this is to enter
evalin('base','z1')
after the input prompt.
doc evalin
for details of this function.
Your problem is a bit odd to me, though. If you already know what z1 is before you call the function, why are you asking the user for it? Why not just pass it as an argument?

5 件のコメント

fatema saba
fatema saba 2014 年 6 月 23 日
I change the function like that: function[f]= ave1(z1,z2) evalin('base','z1') z1=input('input z1 matrix: ') doc evalin [n1,n2]=size(z1); n3=n1*n2 [n4,n5]=size(z2); z2=input('input z2 matrix: '); n6=n4*n5 B=reshape(z1,n3,1); C=reshape(z2,n6n1); f=[A(:);B(:)] end but I got this error: z1 =
12 15 16
31 65 79
20 22 34
>> ave1
z1 =
12 15 16
31 65 79
20 22 34
input z1 matrix: z1 ??? Input argument "z1" is undefined.
Error in ==> ave1 at 3 z1=input('input z1 matrix: ')
input z1 matrix:
the cyclist
the cyclist 2014 年 6 月 23 日
Here is what I meant:
When the code reaches the line
z1=input('input z1 matrix: ')
then the USER has to type
evalin('base','z1')
in order to tell MATLAB to look for z1 in the base workspace, not inside the function workspace.
fatema saba
fatema saba 2014 年 6 月 24 日
Thank you the cyclist. It's great. Is there any other way to introduce matrix into function only by it's name?
Joseph Cheng
Joseph Cheng 2014 年 6 月 24 日
It doesn't look like there is. There is a bit of information from MATLAB on this topic http://www.mathworks.com/help/matlab/matlab_prog/share-data-between-workspaces.html and it shows evalin as what you are looking for.
Is it possible to rewrite portions of your code such that the input portions are before you call ave1(z1,z2)?
example:
%section of code before you need to call ave1()
%z1=[whatever matrix] and z2=[whatever matrix] matrix defined.
z1name =input('input name of z1 matrix: ')
z1name =input('input name of z1 matrix: ')
%then
f=eval(['ave1(' z1name ',' z2name ')']);
you probably want to put some check to verify that the user input of variable names are located in the workspace.
fatema saba
fatema saba 2014 年 6 月 25 日
Excuse me I am beginner. this is my function:
function[f]= ave1(z1,z2)
z1=input('input z1 matrix: ')
[n1,n2]=size(z1);
n3=n1*n2
z2=input('input z2 matrix: ');
[n4,n5]=size(z2);
n6=n4*n5
B=reshape(z1,n3,1);
C=reshape(z2,n6,1);
f=[B(:);C(:)]
end
how can I change it?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by