how would one use logic commands to run separate function files with single-statement intput?
4 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Steven Lord
2024 年 11 月 29 日
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 件のコメント
Steven Lord
2024 年 11 月 30 日
See the Description section on the documentation page for the input function for a description of the 's' option to that function.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Scope Variables and Generate Names についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!