Management of string elements from a text file
Show older comments
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 Comments
dpb
on 28 Nov 2017
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
on 28 Nov 2017
dpb
on 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
on 29 Nov 2017
cbh0307
on 29 Nov 2017
cbh0307
on 29 Nov 2017
Answers (0)
Categories
Find more on Characters and Strings 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!