string to text file

27 ビュー (過去 30 日間)
Matthew Tyler Jeffries
Matthew Tyler Jeffries 2019 年 3 月 8 日
編集済み: per isakson 2019 年 3 月 9 日
Hello,
I have a program that produces a very large string array, and I would like to be able to open it in Notepad++. I tried going into Notepad++ to open my file, but when it opened, it looked like gibberish. How can I convert my matlab string array so I can open it in Notepad++?
Thank you
  8 件のコメント
Matthew Tyler Jeffries
Matthew Tyler Jeffries 2019 年 3 月 9 日
format long
N=input('Enter maximum distance (km) between locations:');
% LLL=table2array(LLL);
% MLL=table2array(MLL);
% MID=table2array(MID);
% LID=table2array(LID);
sizeM=size(MLL);
lengthM=sizeM(1,1);%gives the number of rows in the mindat_LatLong matrix.
sizeU=size(LLL);
lengthU=sizeU(1,1);%gives the number of rows in the UniqueID_LatLong matrix.
p=1;%index variable used to track the row in the mindat_LatLong matrix.
m=1;%index variable used to assign MindID_number to the given UniqueID_LatLong.
v=[];%open matrix for storing mindat id numbers
Results=strings(lengthU,2);
n=1;
z=[];%open matrix for storing distances.
for x=1:lengthU
while p<=lengthM
d=2*6370.997*asin(((sin(((LLL(x,1))-(MLL(p,1)))/2))^2+cos(LLL(x,1))*cos(MLL(p,1))*(sin((LLL(x,2)-MLL(p,2))/2))^2)^0.5);
if 0<d && d<=N
v(1,m)=MID(p,1); %this creates a vector of MindatID numbers.
z(1,p)=d; %this creates a vector of distances.
elseif d==0
v(1,m)=MID(p,1); %this creates a vector of MindatID numbers.
z(1,p)=-5;
else
end
p=p+1;
m=m+1;
end
v(v==0)=[];
z(z==0)=[];
if isempty(v)==0; %if the vector is not empty...
m=min(z);%this finds the minimum distance.
ans = find(z==m);%this givs the column in z than contains m.
index=v(1,ans);%this matches the smallest distance to the corresponding mindatID.
index=mat2str(index);
Str = sprintf('%.0f,' , v);%this separates the values in the string with a comma
Str = Str(1:end-1);
string=[LID(x,1) Str index];
Results(n,1:3)=string;
n=n+1;
end
p=1;
m=1;
z=[];
v=[];
end
Results = rmmissing(Results);
Image Analyst
Image Analyst 2019 年 3 月 9 日
What's all this? Did you see any of the the compact Answers below?

サインインしてコメントする。

採用された回答

per isakson
per isakson 2019 年 3 月 8 日
編集済み: per isakson 2019 年 3 月 8 日
This creates a textfile, which notepadd++ opens.
v1=str(:,1);
v2=str(:,2);
v3=str(:,3);
T = table( v1,v2,v3 );
writetable( T, 'str.csv' )
as does
T = table( str(:,1),str(:,2),str(:,3) );
writetable( T, 'str.csv' )
however, this throws an error
>> writetable( table( str ), 'str.csv' )
Error using writetable (line 142)
Unable to perform assignment because the left and right sides have a different number of
elements.
Better control of the format
%%
cac = str2cell( str );
fid = fopen( 'h:\m\cssm\str.csv', 'w' );
fprintf( fid, '%-24s %-24s %-24s\n', cac{:} );
fclose( fid );
Where (OCR and cleaning)
%%
str = [
"later ite-DD030" "210801" "210801"
"later ite-DD036" "28160" "28160"
"later ite-DDU7" "127198" "127198"
"later ite-DDC4g" "204186" "204186"
"laterite-DDOSO" "19161" "19161"
"later ite-DDD63" "302210" "302210"
"later ite-DDD68" "258159" "214865"
"later ite-DDDEg" "214865" "231473"
"laterite-DD073" "231473" "203491"
"later ite-DD074" "210801" "210801"
"laterite-DDD81" "28160" "28160"
"later ite-DDDB8" "127198" "127198"
"laterite-DOID8" "204186" "204186"
"laterite-DOIDg" "19161" "19161"
"laterite-DOI IO" "302210" "302210"
"laterite-DOI 12" "258159" "214865"
"laterite-DOI 13" "214865" "231473"
"laterite-DOI 16" "231473" "203491"
"laterite-DOI Ig" "203491" "204186"
"podchrome-DDDOI" "228019" "228019"
"podchrome-DDD02" "208319" "208319"
"podchrome-DDDD3" "4653" "252321"
"podchrome-DDDCL4" "251670" "251670" ];
  3 件のコメント
Matthew Tyler Jeffries
Matthew Tyler Jeffries 2019 年 3 月 9 日
編集済み: per isakson 2019 年 3 月 9 日
I tried using the code below, and it worked in Matlab without an error, but when I opened the file 'str.csv', the values were messed up (see picture below)
Thank you
v1=str(:,1);
v2=str(:,2);
v3=str(:,3);
T = table( v1,v2,v3 );
writetable( T, 'str.csv' )
Matthew Tyler Jeffries
Matthew Tyler Jeffries 2019 年 3 月 9 日
I figured it out! I just changed the 'str.csv' to 'str.txt' and it fixed the issue. This is exactly what I need, thank you!

サインインしてコメントする。

その他の回答 (2 件)

Bob Thompson
Bob Thompson 2019 年 3 月 8 日
Take a look here for ways to write data in variables to text files.
  1 件のコメント
per isakson
per isakson 2019 年 3 月 8 日
There is no section on exporting a string array.

サインインしてコメントする。


Image Analyst
Image Analyst 2019 年 3 月 8 日
Are you using %s instead of %f? Try this - it works!
Results = [
"later ite-DD030" "210801" "210801"
"later ite-DD036" "28160" "28160"
"later ite-DDU7" "127198" "127198"
"later ite-DDC4g" "204186" "204186"
"laterite-DDOSO" "19161" "19161"
"later ite-DDD63" "302210" "302210"
"later ite-DDD68" "258159" "214865"
"later ite-DDDEg" "214865" "231473"
"laterite-DD073" "231473" "203491"
"later ite-DD074" "210801" "210801"
"laterite-DDD81" "28160" "28160"
"later ite-DDDB8" "127198" "127198"
"laterite-DOID8" "204186" "204186"
"laterite-DOIDg" "19161" "19161"
"laterite-DOI IO" "302210" "302210"
"laterite-DOI 12" "258159" "214865"
"laterite-DOI 13" "214865" "231473"
"laterite-DOI 16" "231473" "203491"
"laterite-DOI Ig" "203491" "204186"
"podchrome-DDDOI" "228019" "228019"
"podchrome-DDD02" "208319" "208319"
"podchrome-DDDD3" "4653" "252321"
"podchrome-DDDCL4" "251670" "251670" ];
numRows = size(Results, 1);
filename = fullfile(pwd, 'Results.txt');
fileID = fopen(filename, 'wt');
for row = 1 : numRows
fprintf(fileID, '%s, %s, %s\n', Results(row, 1), Results(row, 2), Results(row, 3));
end
fclose(fileID);
winopen(filename); % Pop open in the default text editor program.

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by