Segmenting a Vector into Rows of 8 Characters in MATLAB?

Click For Summary
SUMMARY

The discussion focuses on segmenting a one-dimensional character vector in MATLAB into rows of 8 characters. The user successfully appends blank characters to ensure the vector's length is a multiple of 8 but struggles with the segmentation process. The solution involves using the MATLAB function reshape with the syntax reshape(V, 8, 2)' to achieve the desired 2x8 matrix format. Proper padding of the vector is essential before applying the reshape function to ensure correct segmentation.

PREREQUISITES
  • Understanding of MATLAB syntax and functions
  • Familiarity with character vectors in MATLAB
  • Knowledge of matrix manipulation in MATLAB
  • Basic understanding of string padding techniques
NEXT STEPS
  • Explore MATLAB's reshape function in detail
  • Learn about string manipulation functions in MATLAB
  • Research techniques for padding strings in MATLAB
  • Investigate matrix transposition in MATLAB
USEFUL FOR

This discussion is beneficial for MATLAB programmers, data analysts, and anyone working with string manipulation and matrix operations in MATLAB.

GreenPrint
Messages
1,186
Reaction score
0
Given an arbitrary one dimensional vector v imputed by the user
for example v='Helllllloooooo'
or
v='Helo'
I was able to append empty characters onto anyone dimensional vector that the user can enter so that way the total length of the vector is a multiple of 8 characters of long or not add any empty characters onto the vector if it's length is already an exact multiple of 8

I'm struggling with segmenting this vector's characters to a vector that has as many rows as needed that each have exactly 8 column's

for example
function('Hello World!')
and the function returns
g=Hello Wo
rld!
were it adds blank characters so that there is actually blank spaces after the exclamation mark to make the second row 8 characters in length.

I'm all set on making the input to the function be a multiple of 8 characters in length regardless of what string the user inputs. I'm struggling with segmenting this new message like in the example above because the message can be any multiple of 8... I tried using a four loop and just taking off multiples of 8 and setting them to a new row of another vector but am struggling because of the fact that the message can be any length

basically how do I turn any string who's length is a multiple of 8 (i figured out how to append blank spaces so that it would always be a multiple of 8) and segment it into something like this
function('Hello World!')
and the function returns
g=Hello Wo
rld!

I hope that makes sense
thanks for any help
if it's not clear what I'm trying to do please let me know.

I tried taking any string such as
V='helloooooooooo '
and using
reshape(V,2,8)
but it returns something like this

hlooooo
elooo

which is not what i wanted...
 
Physics news on Phys.org
So you want g to be a 2x8 matrix in the case you listed?
The reshape you used there won't work as reshape goes down columns, not across rows. To fix this though, use reshape(V,8,2)' which gives you the transpose of the reshape vector.
Don't forget to pad out the vector so it's the right size beforehand though!
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
859
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K