How to set up an array given a string input by the user?

6 views (last 30 days)
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!

Accepted Answer

Walter Roberson
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
input_string = "Hello World!"
as_char = char(input_string) %this is a character vector, not a string() object
as_char = 'Hello World!'
circshift(as_char, 4)
ans = 'rld!Hello Wo'
  3 Comments
Walter Roberson
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)
c4 = 'rld!Hello Wo'
cn2 = circshift(as_char, -2)
cn2 = 'llo World!He'
text(0.1, 0.5, c4)
text(0.1, 0.53, cn2)
Lucas Lombard
Lucas Lombard on 7 Nov 2022
I'll ask my professor if that's a route he had in mind when he made up this project, it seems to be a much more direct solution for sure! I think he meant for each letter to have a corresponding "pixel art" in the form of XY coordinate pairs like a 5x5 grid for W = [1 5 1 5 1 3 5 1 2 4 5 1 5 ; 5 5 4 4 3 3 3 2 2 2 2 1 1]; and you scatter plot that large enough that it resembles the letter W. I was doing scatter(W(1,:), W(2,:), 300, 'rs', 'filled').

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!