how would one use logic commands to run separate function files with single-statement intput?

So I am trying to create a ballistic calculator as a class project, and I am trying to make it as user friendly as possible. I have a couple of function files with the equations relevant to the cartridges we made ourselves based on charts and I would like to call on them using logic via input in the command window. I found you can simply run a file by using the run command, but we haven't covered this in class and so the nomenclature is a struggle for me.
the file name is c223 for .223 rem. and i haven't uploaded the other files my teamates are working on. so i have the program display arbitrary text values until i can upload them in. I'm just trying to get this to work.
this is what i have so far, can someone please help me understand how to convince matlab to run the function file by entering ".223" into the command window?
thanks!
%project draft
clear, close all, format compact, clc
disp('select caliber (.223, .270 win):')
run(f)
switch f
case .223
f=c223.m
case .270
cal=disp('ok')
otherwise
cal=disp('no')
end

 Accepted Answer

Use the input function with the 's' option to allow the user to type in a string, or you could use uigetfile to let the user select the file from a dialog box. Once you have the name, one way to run the file is using switch but that would require you to modify your code every time you create a new function for a new armament. Or you could use feval if you allow the user to select a file interactively.
% [file, location] = uigetfile; % can't run in MATLAB Answers, assume that I've selected why.m
file = 'why.m'; % I'm not going to use location in this example
Now remove the extension:
[~, name, ext] = fileparts(file)
name = 'why'
ext = '.m'
and run it:
feval(name) % call the why function with 0 inputs and 0 outputs
To satisfy the rich hamster.

2 Comments

uigetfile and feval are interesting functions and i definitely would like to get comfortable with them. but for clarity's sake, when you "s" option, do you mean like in place of where i have "run(f)"?
thank you so much!
See the Description section on the documentation page for the input function for a description of the 's' option to that function.

Sign in to comment.

More Answers (0)

Categories

Find more on Scope Variables and Generate Names in Help Center and File Exchange

Asked:

on 29 Nov 2024

Commented:

on 30 Nov 2024

Community Treasure Hunt

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

Start Hunting!