Question

In: Computer Science

Write a script that will first initialize a string variable that will store x and y...

Write a script that will first initialize a string variable that will store x and y coordinates of a point in the form ‘x 3.1 y 6.4’. Then, use string manipulating functions to extract the coordinates and plot them. ANS % create a string variable of the form 'x 3.1 y 6.4' % extract the x and y coordinates and plot str = 'x 2.3 y 4.5'; [letter rest] = strtok(str); [x rest] = strtok(rest); [letter rest] = strtok(rest); y = rest(2:end); x = str2num(x); y = str2num(y); plot(x,y,'go')

IN MATLAB I AM TRYING TO USE THIS CODE BUT IS NOT WORKING, may you please use this code and tell what is wrong with it

Solutions

Expert Solution

You have not used comma when using strtok because there are two output from this module. and you need to use str2double in place of str2num.

This is your corrected code

%take the string
str = 'x 2.3 y 4.5';
%extract first word from string and remaining
[letter1, rest] = strtok(str);
%again repeat tha same process
%since second word is numerical coordinate then store it to variable x
[x, rest] = strtok(rest);
%extract first word from string and remaining
[letter2, rest] = strtok(rest);
%at the last only y coordinate remains
y = rest(2:end);
%convert x to numeical value
x = str2double(x);
%convert y to numeical value
y = str2double(y);
%plot the point
plot(x,y,'go');
%label the x axis
xlabel(letter1);
%label the y axis
ylabel(letter2);
%title to the curve
title(strcat(letter2," vs ",letter1));
%add grid
grid minor;

This is code for second string

%take the string
str = 'x 3.1 y 6.4';
%extract first word from string and remaining
[letter1, rest] = strtok(str);
%again repeat tha same process
%since second word is numerical coordinate then store it to variable x
[x, rest] = strtok(rest);
%extract first word from string and remaining
[letter2, rest] = strtok(rest);
%at the last only y coordinate remains
y = rest(2:end);
%convert x to numeical value
x = str2double(x);
%convert y to numeical value
y = str2double(y);
%plot the point
plot(x,y,'go');
%label the x axis
xlabel(letter1);
%label the y axis
ylabel(letter2);
%title to the curve
title(strcat(letter2," vs ",letter1));
%add grid
grid minor;

If you have any problem then let me know in comment box and if you like then please rate or give a big thumbs up thank you.


Related Solutions

Write the following Python script: Problem Statement A string X is an anagram of string Y...
Write the following Python script: Problem Statement A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb". A set of strings is anagram-free if it contains no pair of strings which...
Declare a string variable and initialize it with your first name ( in C++)
Declare a string variable and initialize it with your first name ( in C++)
Write the following Python code: A string X is an anagram of string Y if X...
Write the following Python code: A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb". A set of strings is anagram-free if it contains no pair of strings which are anagrams...
Linux Script Convert String (Part 1) Write a simple shell script that takes a permission string...
Linux Script Convert String (Part 1) Write a simple shell script that takes a permission string expressed as -rwxrwxrwx and prints out whether or not theobject is a directory, file or link. The string is read in from standard input. Convert String (Part 2) Modify the script so its able to print out the octal permission which the string represents along with the file type. This is in addition to part 1. Convert String (Part 3) For the script in...
Matlab Create/write a function that takes an input of x and y data, and a string...
Matlab Create/write a function that takes an input of x and y data, and a string (either linear? or quadratic), sets up a linear system of equations (Ax = b), and solves and plots the model.
Write script in Matlab to solve 1. Y=Ax where X is of dimension n by 1...
Write script in Matlab to solve 1. Y=Ax where X is of dimension n by 1 and A is of dimension m by n. 2. C= A*B where A is of dimension m by p and B is dimension of p by n. So write matlab script to obtain Y and C in above problems.
⦁   Write a Bash script that prompts for user input and reads a string of text...
⦁   Write a Bash script that prompts for user input and reads a string of text from the user. If the user enters a no null (no empty) string, the script should prompt the user to re-enter again the string; otherwise should display the string entered “. ⦁   Given an array of the following four integers (3, 5, 13, 14); write a Bash script to display the second and all elements in the array.    ⦁   Write a short Bas...
Write a complete shell script that first asks the user to enter a URL. The script...
Write a complete shell script that first asks the user to enter a URL. The script should read the URL into a variable named url. The shell script should then retrieve the file associated with the URL by using the curl command. The output of the curl command should be redirected to a file named html_file. The shell script should then use the grep command to search the file named html_file for the word manhattan. Finally, the shell script should...
Write a script le using conditional statements to evaluate the following function, assuming that the scalar variable x has a value. The function is Y = ex+1 fo
Write a script le using conditional statements to evaluate the following function, assuming that the scalar variable x has a value. The function isY = ex+1 for x < -1, y = 2 + cos(πx) for -1 ≤ x ≤ 5, and y = 10(x - 5) + 1 for x ≥ 5. Use your le to evaluate y for x = -5, x = 3, and x = 15, and check the results by hand.
Given a String variable address, write a String expression consisting of the string "http://" concatenated with...
Given a String variable address, write a String expression consisting of the string "http://" concatenated with the variable's String value. So, if the variable refers to "www.turingscraft.com", the value of the expression would be "http://www.turingscraft.com". in java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT