How can I use a variable from another script?

11 vues (au cours des 30 derniers jours)
Thomas Brown
Thomas Brown le 2 Déc 2016
Commenté : Stephen23 le 3 Déc 2016
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');

Réponses (1)

Image Analyst
Image Analyst le 2 Déc 2016
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

Catégories

En savoir plus sur Variables dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by