How do I overwrite a structure using a switch statement.

Hello,
I have the following function:
function [A,N] = area(f,a,b,varargin)
%Structure definition
s = struct('fh',f,...
'a',a,...
'b',b,...
'N',64,...
'Tolerance',10^(-6),...
'Algorithm','MiddleSum');
... rest of the code
The structure contains the defaults values that will be used to run the function if the user doesn't provide values for 'N',Tolerance and Algorithm.
I need to use a SWITCH statement to overwrite the structure values if the user does provide any or all of the values, and it must be case insensitive.
So if the user enters area(X^2,0,2,'N',2,'AlGoRithm','TrapeZoid') the structure should replace the default values N=64, algorithm = MiddleSum, with N=2 and Algorithm = Trapezoid.
Can someone please help me?

Respuestas (1)

Rik
Rik el 23 de Nov. de 2020
You can copy the code I used in my FEX submissions (see WBM, readfile, BlandAltmanPlot or RegGrow).
If you simply want to overwrite the default values you don't need a switch, you can simply use a loop.

4 comentarios

Hi Rik!
I wish I could just do that but my assignment specifically asks for a switch.
Rik
Rik el 23 de Nov. de 2020
If you look at my functions you will see I also did that. The benefit is that you can implement a test as well (validating input). Did you look up the functions I mentioned? Do they solve your question? Or do you need further clarification?
Hi, yes I looked at them. I'm pretty new with Matlab so it's hard for me to understand what's going on there :/ Do you have a simpler code that could help me a little bit?
Rik
Rik el 24 de Nov. de 2020
Do you have trouble finding which function you need to look at? Did you run any example with the debugger (so you can step through the code line by line)?

Iniciar sesión para comentar.

Categorías

Más información sobre Function Creation en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 23 de Nov. de 2020

Comentada:

Rik
el 24 de Nov. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by