return of parameters from matlab script - matlab

let us suppose that we have following function
function [y1,y2,y3,y4]=mystery(a,x);
y1=a*x;
y2=a*x^2;
y3=a^2*x^2;
y4=a*x^3+5;
end
now what i want to make sure is order of result returned from this code,for instance
[y1,y2,y3,y4]=mystery(3,5);
does it return in reverse order or directly in direct form?i meant when m file is executed does it first return last result,then previous of last line and so on?thanks in advance

The parameters are always returned in the order of the declaration. The order of evalutation does not matter. So, in your case, you will always have the order [y1,y2,y3,y4].
Edit:
If you want to access the second or third parameter only, you can do [~,y2]=mystery(1,2) or [~,~,y2]=mystery(1,2) respectively.

Matlab executes line by line in the script. The first line is always executed first.

Related

Matlab script: Display the outputs of my Simulink model

I have the following Simulink model:
I have the following script:
Constant=43;
Constant1=43;
Constant2=43;
Constant3=43;
Constant4=43;
Constant5=43;
Constant6=43;
Constant7=43;
Constant10=43;
Constant11=43;
In1=[1,2,3];
In2=[4,5,6];
t_stop = 10;
T_s = t_stop/1000;
options = simset('solver', 'ode5', 'fixedstep', T_s);
sim('test_lau.slx',t_stop,options)
I want to display and eventually call the outputs Display, Display2 and Display3.
I tried changing the last line to:
[Display, Display2, Display3]=sim('test_lau.slx',t_stop,options)
But get the error:
Number of left-hand side arguments doesn't match block diagram. When specifying that root-level outports are to be returned individually, the number of left-hand side arguments must be 2 (for T,X) plus number of root-level outport blocks
Does anyone know how to extract those outputs from a script?
Thank you in advance for your help!
Thanks to #Ander Biguri and #Wolfie
The answer was using the block "to Worskpace" instead of the block "Display"

Is there a solution to matlab claiming insufficient input arguments when enough are being supplied?

I have a custom function I have made, which requires two input arguments. This is the opening line of the function, clearly showing it needs just two input arugments:
function [file,frame,vertices,edges,faces,faceOrders,edgeOrders] = FOLD_reader(filename,rundocfolder)
The FOLD_reader function is called within another function (FEAfromFOLDVaryingStiffness), using the following line of code:
[~,frame,vertices,~,faces,~,~] = FOLD_reader(filename,rundocfolder);
To which matlab claims:
Not enough input arguments.
Error in FEAfromFOLDVaryingStiffness (line 12)
[~,frame,vertices,~,faces,~,~] = FOLD_reader(filename,rundocfolder);"
However, if I copy and paste the offending line into the command window, it works perfectly. The filename and rundocfolder variables are definitely defined in FEAFromFoldVaryingStiffness which calls FOLD_reader, as they are among the input arguments of the FEAFromFold(etc) function itself.
Has anyone had any experience with this seemingly bizzarre error? To me it makes no sense at all.
If it's a help here are the lines up to the error point inside FEAfromFOLDVaryingStiffness:
function [] = FEAfromFOLDVaryingStiffness(filename,meshsize,displacement,m,n,stiffnessvary,rundocfolder)
%Comments ommitted for brevity
[~,frame,vertices,~,faces,~,~] = FOLD_reader(filename,rundocfolder);
I'm an idiot, I called the FEAfromFOLDVaryingStiffness with one too few arguments, I'd left out m or n or something and so the last input variable (rundocfolder) was undefined, which showed up as an error with fold_reader inside the FEAfromFOLDVaryingStiffness function, instead of where the error actually was: the top level script.

Matlab assignment - return only odd elements

I wrote code similar to that posted by FeliceM, but when I tried to include the recommended changes/additions I got the following error:
Warning: File: odd_index.m Line: 5 Column: 18
Function with duplicate name "odd_index" cannot be called.
Here's my code:
function odd_index
M=[1:5; 6:10; 11:15; 16:20; 21:25];
M=M(1:2:end, 1:2:end);
end
function M_out = odd_index(M)
M_out = M(1:2:end, 1:2:end);
end
Not quite sure what I'm doing wrong.
It appears to be simply a problem of naming your two functions the same name. Rename the second function (and make sure it's in a separate file with the same name as itself). Really, the first one isn't a function at all, but appears to be a script.
You may want to refer to the MATLAB Getting Started help documentation for more information on functions and scripts. http://www.mathworks.com/help/matlab/getting-started-with-matlab.html

Getting two parameters from c function in matlab

i had a code that c send back 1 number (mex)
the matlab code was
vMsg=unit32(Gateway_test_app(2))
now i added 1 more return value to Gateway_test_app(2) which is s STRING
what i need to do to get the two values back
i was thinking about something like this:
[vMsg,errMsg]=??????(Gateway_test_app(2))
what should i put in the ????? place?
thx for any help
johnny.
ps
using codegen and need not to get err when building
First call the function and store the two outputs, then run your extra function unit32 (what does it do, by the way?) on the first output only:
[vMsgOriginal, errMsg] = Gateway_test_app(2);
vMsg = unit32(vMsgOriginal);
This assumes that you don't want to process your new string output through your unit32 function.

Matlab saving without losing previous iteration's value

i'm trying to save values of iterations in a loop. After this function, they will execute other functions before coming to the next iteration. But the problem i'm facing is, it overwrites them and bcomes 000000. Only the last iteration values are seen. How can i fix it ? Is there a way not to use the same variable and save them ? i read about append but will have to use different var name n is not really nice to do so.
function DistanceMatrix(iteration,distance_row)
load('data.mat','oridata')
load('centroids.mat','centroids')
for i = distance_row:(distance_row+3)
for j=1:300 %no.genes
total=0;
for k=1:6
total=total+((oridata(j,k)- centroids(i,k))^2);
end
DistMatrix_Val(i,j)=sqrt(total);
end
end
save('DistanceMatrix.mat','DistMatrix_Val')
DistMatrix_Val;
GroupMatrix(iteration,distance_row)
end
This is the output. I WOULD LIKE TO STORE ALL ITERATION's value and not overwrite them. Can any1 help ?
OK. Use
load('DistanceMatrix.mat','DistMatrix_Val')
or
persistent DistMatrix_Val
just before the first load command you showed to us.
I think this is what you should do:
functon DistanceMatrix = DistanceMatrix(iteration,distance_row)
Rather than saving the variable at the end of the function, you return it.
After returning it you can include the variable in a bigger variable (for example a three dimensional Nx4x300 matrix)
If neccesary you can then save it at the end.