MATLAB Answers

any suggests, solutions ?

1 view (last 30 days)
diadalina
diadalina on 28 Feb 2020
i have a set of parameters, and i want fo create a file function that containt all my parameters in order to use them in others programs, but i can't find the way to do that !

  0 Comments

Sign in to comment.

Answers (2)

Jeremy Marcyoniak
Jeremy Marcyoniak on 28 Feb 2020
I changed one of them for you and re-attached it here.

  3 Comments

Jeremy Marcyoniak
Jeremy Marcyoniak on 28 Feb 2020
Although, you could just make it a script file and call it like it's a function.
diadalina
diadalina on 29 Feb 2020
but how do i call the file as the way that you have done ?
Jeremy Marcyoniak
Jeremy Marcyoniak on 2 Mar 2020
% main file
paramteres
alternatively, if you used a script
% main file
run(paramtres)

Sign in to comment.


dpb
dpb on 28 Feb 2020
Jeremy is correct...
ncparamteres.m would contain:
%ncparamteres.m
% Script to create variables in the calling MATLAB workspace
nc = 500;
T = 365;
t = 0:T/(nc-1):T;
I=500;
...
% the rest shouldn't be hard to decipher... :)
Alternatively, create an Initialization routine that is run once and saves the desired variables in a .mat file -- then just load that file and all the variables will appear like magic.

  5 Comments

Show 2 older comments
dpb
dpb on 29 Feb 2020
Everything you use has to be already defined in the script/function where you call the new code before you can use it.
So, the lambda values need to have been defined before you can execute that line to use them.
In a coding style observation, having variables of the same name with sequential suffixes to distinguish them is often a sign those variables should be an array. That isn't the reason MATLAB errors, that's because the particular variable is not yet defined, but as soon as you define it you're then undoubtedly going to get the error that lambda3 and maybe the other variables in the line aren't defined, either.
diadalina
diadalina on 29 Feb 2020
i'm trying another way to solve my problem: save the parameters as file.mat and trying to load them , M Matlab gives also an error, please help me:
Subscripted assignment dimension mismatch.
Error in SysDCH (line 3)
f(1,1)=I-(Philambda+muN)*Y(1)+tau*Y(2);
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in pp (line 27)
[~,Y] = ode45(@SysDCH,t,[cD0 cDg0 cDa0 cDh0 cDcc]);
dpb
dpb on 29 Feb 2020
PLEASE! USE THE CODE Button to format the code (or highlight the code and presse CTRL_e)
That error has nothing to do with load, you're trying to assign a RHS of the assignment that is not compatible to the LHS which says the RHS has to be one value because you wrote a subscript of (1,1) on f.
But, we have no way to know what I, Philambda, and muN are as far as their dimensions; one must presume one or more of them is not just a single value but an array or vector.
However, going clear back to your first m-file, we can see that in it
Philambda=(1-lambda)./(1+((1-lambda)/P0-1)*exp(-alpha*t));
and t was a vector so presuming the same definition still holds, Philambda is the same size as t and therefore you're trying to stuff 500 values into one location. No can do.
Use the debugger and consider what you're really doing including whether you have and want arrays or values, don't just throw code at MATLAB and then expect somebody else to figure out what's wrong.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!