How to add an element to an entire array?

4 views (last 30 days)
Ada
Ada on 24 Jul 2023
Edited: dpb on 26 Jul 2023
I'm looking to add an apostrophe to a 1x27 array, so that when it writes into an Excel file, it doesn't remove the leading zeros. Importantly, I am not attempting to add values to the array; my goal is to modify the values already present in the array to precede them all with an apostrophe. I have tried other methods to allow preceding zeros to remain, but none work, as the leading zeros are removed when I do anything else. Any advice is greatly appreciated.

Accepted Answer

Ayush
Ayush on 25 Jul 2023
Edited: Ayush on 25 Jul 2023
Hi @Ada,
One thing you can try is storing elements of array in form of string and then you can add apostrophe to them.Then when you want to use the numeric value, just remove the apostrophe and typecast the string back into the numeric value.
For typecasting string to integer, you can use str2num()
Hope it helps!
  3 Comments
Voss
Voss on 25 Jul 2023
vec = [0 1 2 3 4];
str = strcat("'",string(vec))
str = 1×5 string array
"'0" "'1" "'2" "'3" "'4"
Ada
Ada on 25 Jul 2023
Thank you @Voss! I knew I was missing something simple. A little reworking of how I called array values and this worked like a charm.

Sign in to comment.

More Answers (1)

dpb
dpb on 25 Jul 2023
You're going to have to illustrate what you want -- and best would be to provide a small sample code that illustrates the issue that folks here can run.
But, if the issue is how to display a numeric field in Excel with leading zeros, the answer is to use a custom numeric format. A format of "000000" will display integers in a zero-filled width of six and still treat the values as numeric.
If you turn them into text, you'll probaby rue having done so; Excel isn't very happy about text that looks like a number.
There's no way to "add" an apostophe to a numeric value in MATLAB, numbers are numbers and their internal storage is NOT what a visual representation looks like; you can do the same thing to convert the array to text with compose or num2str and then write the text representation, but that will have the same issues inside Excel itself.
Context on why it matters might help produce more/other answers...
  2 Comments
Ada
Ada on 25 Jul 2023
Edited: Ada on 25 Jul 2023
To better clarify my goal:
I do know that I cannot modify a numeric value with non-numerics within MATLAB. My goal is to modify each element of the array so that they are strings, and then precede each of those strings with an apostrophe so that, when exported into Excel, the leading zeros will be preserved. Effectively, my goal is to take some string, a = [0 1 2 3 4], and modify it so it becomes a = ['0 '1 '2 '3 '4]. I could not get num2str to work before, but I will reattempt and return with a code example, assuming I cannot get it to work again.
It matters because the data being output to the file is binary, and the leading zeros are important for expressing the 8 bits once they are read again by a future device. If the leading zeros are missing, the expected input won't be met, and it'll throw an error.
I didn't want to use a custom numrmic format because I do not know how to set one in Excel from within MATLAB, using MATLAB's code. The purpose of this code is that it will operate on any generic device, so I cannot assume the same file will be used. In effect, I need the code to operate with the expectation that it will be someone's first time launching the code, without need to externally modify parameters in a different program.
dpb
dpb on 25 Jul 2023
Edited: dpb on 26 Jul 2023
"...because I do not know how to set one in Excel from within MATLAB, "
That would have to be done with the COM ActiveX server actxserver and the magic incantations to do so from the VBA equivalents. It's not terribly difficult overall, but can be extremely frustrating to work ones way through the arcane Excel doc's to actually make things work.
" the data being output to the file is binary, and the leading zeros are important for expressing the 8 bits "
Aha! Why didn't say so in the beginning! :)
a=randi(255,5,1)
a = 5×1
10 63 81 168 245
t=strcat("'",dec2bin(a,8))
t = 5×1 string array
"'00001010" "'00111111" "'01010001" "'10101000" "'11110101"
or, more general with using newer features
compose("'%s",dec2bin(a,8))
ans = 5×1 string array
"''00001010" "''00111111" "''01010001" "''10101000" "''11110101"

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!