Management of string elements from a text file

Hello ladies and gentlemen, I have encountered several problems when trying to read a text files and try to change it to text files with new format of display. I will simplify the table to make it clearer. The table somehow looks like this (without first column of titles):
and this table should be changed to new table that looks like this to be later saved in another text file:
The 'x' will be added behind to the hexadecimal address (string), and some of the hex data got only 1 content instead of 2 (empty space which should be considered as well)
I have tried in the first step of reading the data in string format %s, the codes look like these:
File_Input = 'C:\Users\Test.txt';
formatSpec = '%s %s %s %s %s';
Array_Old = textscan(fileID, formatSpec);
Array = zeros(numel(Array_Old{1, 1}), size(Array_Old, 2));
for i = 1:size(Array_Old,2)
for j = 1:size(Array_Old{1, i}, 1)
Array(j,i) = Array_Old{1, i}{j, 1};
end
end
fclose(fileID);
The Array_Old is 1x5 cell (there are vectors inside every 5 cells), while the Array is 5x5 double. I cannot pass the old value to the new value as it is double to cell. After that I try to use strings instead of zeros to create empty array for string, but encountered the problem:
Attempt to execute SCRIPT strings as a function:
C:\MATLAB\R2016a\toolbox\matlab\strfun\strings.m
It would be pretty much appreciated if anyone has a better insights on how to solve this problem. Thank you in advance.

6 commentaires

What's form of input text file? If not delimited, textscan won't read the empty records as null fields altho there is finally in R2016b(?) a new function to read fixed-format files.
Once around that, it appears all the data are hex values, if you read as numeric with '%X' format string then can write with it as well...the trailing 'X' is essentially trivial...
>> h=hex2dec('FF3C'); % builtin to get value in memory...
>> sprintf('%Xx',h) % print hex with trailing x
ans =
FF3Cx
>> sscanf(ans,'%X') % read the hex value as numeric from text
ans =
65340
>>
cbh0307
cbh0307 le 28 Nov 2017
Thank a lot dpb! Your answer is very helpful. Talking about the version, I am thinking that I got problem running 'strings' as my MATLAB version is R2016a. Yes, about the empty space, I think I need to set condition which extract the last element in a row, and the other as content.
Actually I have problem assigning the values of what I read, "Array_Old" into the array of which I would like to work on, "Array". Since with initialization method , 'zeros', I got everything in double. If I do the for-loop as shown above, the 0 will be read as 48 (0 in ASCII-table), and for others values got problem, since i read it with %s. If strings (to initialize and create empty array for STRING) is not available, is there other method that I can work on the format of Array or Array_Old, so that the value can be assigned?
dpb
dpb le 28 Nov 2017
Well, yes, but why? Again, show us the actual form of the input file itself and let's fix the problem from the git-go instead of trying to fix a problem that doesn't need to be there in the first place...
cbh0307
cbh0307 le 29 Nov 2017
I am sorry I have missed the part you have asked about the input form. It's a file with every column delimited by tab, the empty records are just space bar, and for the next line the new line is used. The delimiters I would used for textscan are ' ', '\t' and '\n', if they are needed.
cbh0307
cbh0307 le 29 Nov 2017
I will attach a picture here:
I have updated my MATLAB version from R2016a to R2017b and replace the following commands with the next one
Array = zeros(numel(Array_Old{1, 1}), size(Array_Old, 2)); % old
Array = strings(numel(Array_Old{1, 1}), size(Array_Old, 2)); % new
And the reading part of the data into string array worked perfectly. =)

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Question posée :

le 28 Nov 2017

Commenté :

le 29 Nov 2017

Community Treasure Hunt

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

Start Hunting!

Translated by