Converting an old Matlab code from 2011 to work with newest version

24 views (last 30 days)
Hello all
I have very limited Matlab skills as I have used it just couple of times in some courses and mainly for running some premade code. Now however, our paths crossed again and this time for personal hobby project. I am in progress of building a xylophone/marimba for my kid, who has shown some interest in musical instruments. There is a remarkably good xylophone building blog post that I have been reading, which goes through the project in great detail. This blog mentions a Master’s thesis project that focuses on calculating the shape of the curved under side for each bar so that they will produce harmonic timbre. The blog doesn’t include the code itself, but guides the reader to the thesis, which can be found here. The full code is included in the appendix section. I made a pdf to text conversion for the code, fixed the most obvious conversion errors and some warnings that Matlab gave, but there are some functions that my beginner skills cannot handle. I will post those tricky parts here as it was not good practice to paste the whole project. I would greatly appreciate any help and I hope this would also help others.
I currently have Matlab R2020b installed on my work computer, but I can update it to R2022b once I will be back in uni network in early January.
Zhao Mingming's Master's thesis: Automatic Multi Modal Tuning of Idiophone Bars
The pdf to text converted code is in the attachments
The first tricky part is on the page 125 of the thesis, in while loop that begins from the previous page
while any(abs(df)>tol)
%displys the resulted natural frequencies
frequency_old=eval([method'(profile,coeffs,E,G,rho,x_section,nu,d0,width,bar_length,number_of_elements,tol,number_fn,ploton)']);
slope=[];
%for the first three natural frequencies
for k=1:3
coeffs_new=coeffs;
coeffs_new(k)=coeffs_new(k).*delta_a;
frequency_new=eval([method'(profile,coeffs_new,E,G,rho,x_section,nu,d0,width,bar_length,number_of_elements,tol,number_fn,ploton)'])
slope=[slope (frequency_new-frequency_old)./(coeffs_new(k)-coeffs(k))];
end
df=frequency_required-frequency_old;
da=(slope)\df;
coeffs=coeffs+da;
end
I get the following error. I have tried to fix it myself, but no luck so far. This eval([method... appears many times in the program so I think it will get me going if I know how to fix it.
frequency_old=eval([method'(profile,coeffs,E,G,rho,x_section,nu,d0,width,bar_length,number_of_elements,tol,number_fn,ploton)']);
Error: Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.

Accepted Answer

Stephen23
Stephen23 on 23 Dec 2022
Edited: Stephen23 on 23 Dec 2022
"I have very limited Matlab skills ..."
Don't learn from that badly-written code. Use function handles!
"This eval([method... appears many times in the program so I think it will get me going if I know how to fix it."
The shown code would also throw an error in earlier versions, so it is possible it was transcribed incorrectly: my guess is that there was a space or comma between METHOD and the following character vector:
[method,'(profile,coeffs,...etc...number_fn,ploton)']
% ^ missing comma
But you should avoid writing/copying code that uses EVAL() to perform basic tasks like this.
Better:
A much better approach is to call FEVAL() like this:
frequency_old = feval(method, profile,coeffs,E,G,rho,x_section,nu,d0,width,bar_length,number_of_elements,tol,number_fn,ploton);
Best:
Using a function handle will make your code more robust and much more efficient than that badly-written code:
For example, in that Mfile you should replace this line (assuming that METHOD_TIMOSHENKO is a function):
method = 'Method_Timoshenko'; % needs to be EVALed later... ugh :(
with this function handle:
method = @Method_Timoshenko; % this defines a function handle!
and then later replace that ugly evil EVAL() call with this much more efficient, more robust, simpler code:
frequency_old = method(profile,coeffs,E,G,rho,x_section,nu,d0,width,bar_length,number_of_elements,tol,number_fn,ploton);
Note how the function handle can be simply called, just like other functions.
  1 Comment
Lauri Paulamäki
Lauri Paulamäki on 23 Dec 2022
Thank you very much for your swift and helpful reply. I'll try to digest this information and ask again if I don't get it.

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!