Need help with calling on a variable as input for a function
4 ビュー (過去 30 日間)
古いコメントを表示
I am trying to input a variable from the workspace into my function, but am having trouble. I want to run this code numerous times for the different variables, but only want to input a number for the variable. So, my variables are something like A_1_1, A_1_2, ..., A_1_n (where n is the number of variables) and I want to do myfunction(3,1) for example to call on A_1_3. This is necessary because I have other data in the workspace that I want to call on that is named A_5_1, A_5_2, and so on.
I have tried stringing it all together by converting the numbers input into the function into strings and concatenating, and then using eval and genvarname, but this results in an error when I run it as a function (though not when I run it outside of the function). So my code is something like:
function [a, b] = myfunction(n1,n2)
firststr = strcat('myvar_',num2str(n2));
secondstr = strcat(firststr,'_');
m = strcat(secondstr,num2str(n1));
red = [eval(genvarname(m),'=m')];
which results in the error "The expressionn to the left of the equals sign is not a valid target for an assignment" on line 5 in the above code. This error is not given if I run the same code outside of the function and put in the numbers wanted instead of n1 and n2. (red is created as desired this way). Is there a clear reason as to why it doesn't work in the function, or should I try and avoid using eval?
If the latter, I still need help. I have already tried avoiding using eval as many people have suggested to do so on the internet. I have tried loading the data in as a structure, but am not savvy enough to do so without encountering more errors. I have the data file 'datafile.mat' which contains all of the A_1_1, A_1_2,..., A_1_n variables. How can I load it in as a structure and then access the wanted variables as described above? So I can avoid using eval and the messy way of creating variables from strings to call on them.
0 件のコメント
採用された回答
dpb
2015 年 4 月 2 日
編集済み: dpb
2015 年 4 月 2 日
Simple enough...I wouldn't have generated the variables with such names to begin with (and you're beginning to see why)--but if you've already got them in the .mat file then simply read them into a structure with the functional form of load instead of command form. You can then process them as you wish dynamically. A simple illustration--
>> A1=rand(5); A2=rand(5); save A A* % a couple of poorly-chosen numerical variables
>> clear A* % just to prove we're getting them back again...
>> A=load('A') % load all the A-sub-n variables into structure A
A =
A: [1x1 struct]
A1: [5x5 double]
A2: [5x5 double]
>> nms=fieldnames(A); % retrieve the field names for dynamic use...
>> for i=1:length(nms) % we'll just show 'em here...
A.(nms{i}),end
ans =
A1: [5x5 double]
A2: [5x5 double]
ans =
0.5606 0.8790 0.9900 0.4981 0.5860
0.9296 0.9889 0.5277 0.9009 0.2467
0.6967 0.0005 0.4795 0.5747 0.6664
0.5828 0.8654 0.8013 0.8452 0.0835
0.8154 0.6126 0.2278 0.7386 0.6260
ans =
0.6609 0.5814 0.8627 0.6299 0.4896
0.7298 0.9283 0.4843 0.0320 0.1925
0.8908 0.5801 0.8449 0.6147 0.1231
0.9823 0.0170 0.2094 0.3624 0.2055
0.7690 0.1209 0.5523 0.0495 0.1465
>>
NB: the parens surrounding the fieldname which was retrieved in this case from the cell array with the curlies {} to dereference the cell content. You can build these names dynamically from strings to match previously and use them as shown.
In reality, I'd probably have used either a cell array or a data table or even a similar but more amenable-to-handle structure instead, but this should get you around your immediate dilemma.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!