how would one use logic commands to run separate function files with single-statement intput?
31 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jett
am 29 Nov. 2024 um 19:10
Kommentiert: Steven Lord
am 30 Nov. 2024 um 0:53
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
0 Kommentare
Akzeptierte Antwort
Steven Lord
am 29 Nov. 2024 um 20:19
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)
and run it:
feval(name) % call the why function with 0 inputs and 0 outputs
2 Kommentare
Steven Lord
am 30 Nov. 2024 um 0:53
See the Description section on the documentation page for the input function for a description of the 's' option to that function.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Entering Commands finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!