How can I use a variable from another script?

14 次查看(过去 30 天)
Hi all, I am very new to matlab so please bear with me... I want to make a simple speed-distance calculation on another script and use the answer in an sprintf function. I understand I can do this within the sprintf function itself, however the project I am doing specifies I must use a sub-function and this is the simplest way I think. At the end of the sprintf function you can see ''nums1(1,2)/nums2(1,2)'' which is the calculation itself, I want to replace this with the answer from the seperate function.
if choice == 2;
choice = menu('How are you travelling?','Walking', 'Space Shuttle');
if choice ==1
[nums1, txt1] = xlsread('Planet Distance.xlsx','Distance');
[nums2, txt2] = xlsread('Planet Distance.xlsx','Velocity');
answer = sprintf('You are travelling from %s to %s, this journey is %d Km or %d Miles. You have chosen to travel by %s, which on average is at a speed of %d Km/h or %f mph. You will arrive at your destination in %f hours.', txt1{2,1}, txt1{2,2}, nums1(1,1), nums1(1,2),txt2{2,1}, nums2(1,1), nums2(1,2),nums1(1,2)/nums2(1,2) );
menu(answer,'Plot Distance v Time graph');

回答(1 个)

Image Analyst
Image Analyst 2016-12-2
If it's a script, the variables are in the base workspace and your second script can just start using them. If either is a function instead of a script, then you can see the FAQ for ways to share variables. http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F

类别

Help CenterFile Exchange 中查找有关 Variables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by