ERROR unable to convert 'string' value to 'int64'

105 views (last 30 days)
So I am trying to extract data from a .txt file in which the name is provided by the user, change one column, and create a new .txt file with the same data including the alterations. I figured that much out and have managed to create the file, but I am having the hardest time getting my data to properly enter into the new file. I want four distinct columns for each of the data. This is my code below.
fileOriginal = fopen(fileName,'r');
if fileOriginal == -1
disp('The file cannot be found')
nameOriginal = input('Please enter the name of the ASCII text file in which data will be retrived from: ','s');
else
C = textscan(fileOriginal,'%d %s %d %d %d','HeaderLines',1);
end
Yr = C{1};
Mnth = C{2};
LowTemp = C{3};
HighTemp = C{4};
Precip = C{5};
Yr = 1900 + Yr;
fileNew = fopen(newFileName,'w');
Mnth = string(Mnth);
T = [Yr Mnth LowTemp HighTemp Precip];
fprintf(fileNew,'%i %s %i %i %i',T);
When I try to use fprintf to insert my altered into the new file I created, I keep getting an error that says "Unable to convert 'string' value to 'int64'." although I don't understand where it is being converted to int64. Any help is appreciated.

Answers (2)

dpb
dpb on 15 Mar 2019
Yr = 1900 + Yr;
fileNew = fopen(newFileName,'w');
Mnth = string(Mnth);
T = [Yr Mnth LowTemp HighTemp Precip];
You've got a number in Yr and a string in Mnth and are trying to put those together into a single vector with []. In a nutshell, "You can't DO that!"
fprintf and friends take multiple arguments...
fprintf(fileNew,'%i %s %i %i %i',Yr,Mnth,LowTemp,HighTemp,Precip);
  3 Comments
dpb
dpb on 15 Mar 2019
We can't reproduce your code because we don't have the file...need at least the result of what
whos Yr Mnth LowTemp HighTemp Precip
returns for the bare minimum...
Wouldn't hurt to show what
which fprintf -all
returns, just in case you've somehow aliased it...
Hannah Lamon
Hannah Lamon on 15 Mar 2019
This is the information stored in my original file...
Yr Mnth LowTemp(°F) HighTemp(°F) Precip(in)
89 Feb 53.8 71.6 2.67
89 Mar 58.5 76.3 2.84
89 Apr 62.4 80.6 1.80
89 May 68.9 86.3 2.85
When I run the whos command this is what displays...
Name Size Bytes Class Attributes
HighTemp 4x1 16 int32
LowTemp 4x1 16 int32
Mnth 4x1 344 string
Precip 4x1 16 int32
Yr 4x1 16 int32
And this is what results when I run the which command...
built-in (C:\Users\Hannah\Desktop\MATLab\toolbox\matlab\iofun\fprintf)
C:\Users\Hannah\Desktop\MATLab\toolbox\shared\instrument\@icinterface\fprintf.m % icinterface method
C:\Users\Hannah\Desktop\MATLab\toolbox\matlab\serial\@serial\fprintf.m % serial method
Thank you for your help!

Sign in to comment.


dpb
dpb on 15 Mar 2019
Name Size Bytes Class Attributes
HighTemp 4x1 16 int32
LowTemp 4x1 16 int32
Mnth 4x1 344 string
Precip 4x1 16 int32
Yr 4x1 16 int32
Aha! What you didn't tell us (but should have guessed) is that the variables are arrays...and you have output each _array_ in sequence, not an element from each array in turn. Consequently, the first Yr value is output to the '%i' format specifier but the next data to be output is Yr(2) and that won't fit in a string format specifier.
Remember, Matlab is vectorized and each reference to a variable that is an array or vector without subscripting returns the entire array. What you need/want is--
Yr = 1900 + Yr;
fileNew = fopen(newFileName,'w');
Mnth = string(Mnth);
fmt=['%4d %5s' repmat('%6.2f',1,3) '\n'];
for i=1:size(Yr,1);
fprintf(fileNew,fmt,Yr(i),Mnth(i),LowTemp(i),HighTemp(i),Precip(i))
end

Categories

Find more on Object Save and Load in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!