Function input and outputs , variable error, concatenation
3 次查看(过去 30 天)
显示 更早的评论
I am relatively new to MATLAB and I am having some issues with using a function. I am including the code below and will direct you step by step "in parentheses".
"I want my function to work on many files from a folder set of data I have."\
wd='C:\Users\jcb212\Desktop\Pair 3\';
IVlist=dir([wd '*IV*.mat']);
temp=[]; bigarray=[];
for i = 1:length(IVlist)
LD=load([wd IVlist(i).name]);
data=LD.data;
temp=onerunoneIV;
bigarray=[bigarray;temp];
end
"I want each file (i) to be loaded into the loop and run with the function I created. Then I am trying to concatenate all the outputs to an array that I can plot (bigarray) To save on space and reading I will only include the beginning and end of my function."
function [CC12x,CC21x,Rrest1x,Rrest2x,Rin1x,Rin2x,V_rest1x,V_rest2x]=onerunoneIV(i)
output= vertcat (CC12x,CC21x,Rrest1x,Rrest2x,Rin1x,Rin2x,V_rest1x,V_rest2x);
end
"I define all outputs within my function. The error message it gives me = Undefined function or variable "data". I thought I had already named this variable just before I called the function. The variable data is a 22000x4x16 array and I want to call on it in the function. If i need to call it in the function then how to I call my IVlist(i)....any help would be great!"
0 个评论
回答(2 个)
Walter Roberson
2013-6-10
Your function onerunoneIV expects to be passed a value "i". However because you do not use "i" inside the code, the fact that you do not pass "i" in your call to the function will not cause any error in practice.
Your function onerunoneIV expects to use 8 different variables. Unfortunately it does not create those variables and does not have them passed in, and they are not in any nested scope, so the program will crash when it tries to do the vertcat().
If, somehow, the vertcat() succeeded, you would create a variable named "output". That is not one of the variables listed on the left-hand side of the "=" for the "function" line, so that variable would be discarded when the function returned.
Your function onerunoneIV expects to create 8 different output variables. Unfortunately it does not create those variables and does not have them passed in, so they will not be available to return any value so the function would crash upon trying to return.
You define 8 different output variables for the function, but you only use a single output variable, which you call "temp". That is legal in MATLAB, but in your case indicates that you do not understand function outputs. Outputs are positional, so "temp" (outside the function) would get assigned the first output from the function, "CC12x"
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!