Hello, I am having trouble making a script that will show a table of values

The assignment states: Make a script that stores the data in force and distance vectors and computes the corresponding work values (no input and no screen echo). Use disp() to generate a table showing all the force, distance, and work values with appropriate labeling and units. Use appropriate format strings to get the numbers to line up. The data is as shown:
Segment: 1 2 3 4 5
-------------------------------------------
Force(N): 400 550 700 500 600
Distance(m): 3 0.5 0.75 1.5 5
Here is what I try but I always get an error:
Force = [400 550 700 500 600];
Distance = [3 0.5 0.75 1.5 5];
disp(' ')
disp(['Segment ',num2str(1:5,'%10i') ])
disp(['Force, N ',num2str(Force, '%10.0f') ])
disp(['Distance, m ',num2str(Distance, '%10.0f') ])
disp('Segment', num2str(1:5))
disp([ char(8*ones(2,5), num2str([Force;Distance]) ])
disp(' ')

2 commentaires

Stephen23
Stephen23 le 27 Fév 2018
Modifié(e) : Stephen23 le 27 Fév 2018
@Lizzy Murphy: this is not twitter, so I removed the unnecessary # from the tag. I also formatted your code correctly for you: in future you can do this simply by selecting the code text, the clicking the {} Code button, followed by an empty line.
"Here is what I try but I always get an error:"
What is the complete error message that you get?
Hey! I'm sorry about that, it's my first time asking a question on here haha! So after I entered in that data, I wanted to see if my script worked so I typed in the title of the script into the Command space(title of my script is untitled*) So I got this error:
>> untitled*
untitled*
Error: Expression or statement is incomplete or incorrect.

Connectez-vous pour commenter.

 Réponse acceptée

When you run the code (by clicking the green Run triangle on the tool ribbon), it should ask you to save it to an m-file. Call it question5.m or whatever you want, instead of untitled. You can't have asterisks in the name of the function or the m-file. Then to run it again, you can click the green triangle again, or type the m-file name onto the command line (like you tried):
>> question5

3 commentaires

Hey! I see what you mean, it did run, but when it did I came across another error message I think their might be something wrong with my code:
>> Question5
Segment 1 2 3 4 5
Force, N 400 550 700 500 600
Distance, m 3 0 1 2 5
Error using disp
Too many input arguments.
Error in Question5 (line 7)
disp('Segment', num2str(1:5))
Wait! I figured it out:
Force = [400 550 700 500 600];
Distance = [3 0.5 0.75 1.5 5];
disp(' ')
disp(['Segment ',num2str(1:5,'%10i') ])
disp('-----------------------------------------------------------')
disp(['Force (N) ',num2str(Force, '%10.0f') ])
disp(['Distance (m) ',num2str(Distance, '%10.0f') ])
disp(' ')
Then I got this when I ran it!
>> Question5
Segment 1 2 3 4 5
-----------------------------------------------------------
Force (N) 400 550 700 500 600
Distance (m) 3 0 1 2 5
Thank you for your help!!
You might want to switch to using fprintf() instead of disp(), like I do. It gives you more flexibility, plus it's like most of the other languages out there, like C++ etc.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by