passing app inputs to an array in another function

2 ビュー (過去 30 日間)
Timothy Dunning
Timothy Dunning 2022 年 12 月 6 日
回答済み: Timothy Dunning 2022 年 12 月 6 日
I have developed a relatively simple program to simulate a trajectory of a shuttle through the air which had no errors. I then decided to make a GUI so that a user could input what they wanted as initial values. for want of a better way I decided to save the user inputs as global variables but when I try to define my initial matrix z(:,1) I get the following error:
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Here is a snippet of the function where the error occurs:
function guess = shootingMethod()
% set initial value
t(1) = 0;
n=1;
count=1;
dt=0.1;
global z1;
global z2;
global desh;
global errAll;
%initial angle guesses
guess(1)=20;
guess(2)=70;
z(:,1) = [z1;z2*sind(guess(count))]; %error on this line
q(:,1) = [0;z2*cosd(guess(count))];
the global variables z1 and z2 are 1 and 2500 respectively. Where is the error coming from and is there a better way to pass the variables (as I am aware global variables are an ineffiecient method)?
  2 件のコメント
Bora Eryilmaz
Bora Eryilmaz 2022 年 12 月 6 日
編集済み: Bora Eryilmaz 2022 年 12 月 6 日
You can always make your function take the arguments it needs:
function guess = shootingMethod(z1, z2)
end
and call it as
z1 = 1;
z2 = 2500;
shootingMethod(z1, z2)
Timothy Dunning
Timothy Dunning 2022 年 12 月 6 日
yeah but I want z1 and z2 to be defined by the user via the app. 1 and 2500 is just an exmple for this case. the app function cant be changed to allow for passing in and out right?

サインインしてコメントする。

採用された回答

Timothy Dunning
Timothy Dunning 2022 年 12 月 6 日
turns out user inputs from text boxes are stored as strings so the array was attempting to store the list of characters of the number and not just th evalue of the number. I used str2double(value) and it all worked out.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by