How to set up an array given a string input by the user?
6 views (last 30 days)
Show older comments
Lucas Lombard
on 7 Nov 2022
Commented: Lucas Lombard
on 7 Nov 2022
Hi all,
This may be a complicated question, but I'm working on a semester project for a Matlab class I'm in and I have been given a unique problem that I'm not certain how to overcome, so I'm poking the hivemind for ideas on how to tackle this. I need to create a "moving message display" (think like a scrolling LED sign you can program to display things) but using a scatter plot of points that scroll around a plot window. Where I'm getting hung up at the moment is how to tackle parseing the user input. I thought sscanf or cellstr might work but I'm not seeing anywhere in the documentation for either of those that they can break a string down into individual characters and return an array containing the separated letters.
IE if the input string is "Hello World!" the returned array should be a 1x12 array str = [H e l l o W o r l d !] where each character occupies its own cell in the array for the next step (which would be assigning XY coordinate sets to visualize each letter and concatenating that into one continuous array of points to circshift in the scatter plot to make it "scroll").
Would it just be easier to just ask the user to input the starting string as an array of values instead? I was trying to make the input more user friendly but I'm not seeing a method to properly parse a string input into individual characters. I've got about a month to figure something out so this is only an initial attempt before class later this week, so any advice is appreciated.
Thanks!
0 Comments
Accepted Answer
Walter Roberson
on 7 Nov 2022
Character vectors are already arrays of characters.
input_string = "Hello World!" %this is a string() object, not a character vector
as_char = char(input_string) %this is a character vector, not a string() object
circshift(as_char, 4)
3 Comments
Walter Roberson
on 7 Nov 2022
You could assign a fixed height and width for each character and text() each character into position individually.
Or you could assign a fixed height (or width) (not both) to each character and text() each row or column into place using a fixed-width (monospaced) font.
Or you could use a proportional-spaced font (default) and let the characters take variable width (might look more natural).
If you are working with proportional spaced-font, you might want to worry about VerticalAlignment or HorizontalAlignment when you text(); in particular test what happens when a space ends up at the beginning or end of the character vector.
input_string = "Hello World!"; %this is a string() object, not a character vector
as_char = char(input_string); %this is a character vector, not a string() object
c4 = circshift(as_char, 4)
cn2 = circshift(as_char, -2)
text(0.1, 0.5, c4)
text(0.1, 0.53, cn2)
More Answers (0)
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!