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 a PHP script that checks a word or a phrase (stored in a string variable)...
Write a PHP script that checks a word or a phrase (stored in a string variable) to determine if it is a standard palindrome, a perfect palindrome, or not a palindrome at all. Also, for each letter that the word/phrase contains, count and print the number of times that each consonant and vowel is encountered. Your output will look as follows: Word/phrase: racecar Perfect palindrome Contains consonants: r - 2 c - 2 Contains vowels: a - 2 e -...
A) Declare a String variable called myString0 and initialize it to "Java". B) Declare a String...
A) Declare a String variable called myString0 and initialize it to "Java". B) Declare a String variable called myString1 and initialize it to "Programming". C) Declare a String variable called myString2 and initialize it to "Language". D) Print the concatenation of myString0 + " is a " + myString1 + " " + myString2 + ".". E) Print the sum of the lengths of myString1 and myString2 (must call the length() method). F) Print the 2nd, 4th, and 7th character...
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...
Create and initialize a string array, write and use 3 functions.Write a program. Create a string...
Create and initialize a string array, write and use 3 functions.Write a program. Create a string array in the main(),called firstNameArray, initialize with7 first namesJim, Tuyet, Ann, Roberto, Crystal, Valla, Mathilda Write a first function, named searchArray, that passes two arguments: a single person’s name and the array reference, into a function,This function should then search the array to see if the name is in the firstNameArray. If found, it will return the array index where the name is found,...
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.
Phython: Given a string x, write a program to check if its first character is the...
Phython: Given a string x, write a program to check if its first character is the same as its last character. If yes, the code should output "True"
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT