string to text file

27 visualizaciones (últimos 30 días)
Matthew Tyler Jeffries
Matthew Tyler Jeffries el 8 de Mzo. de 2019
Editada: per isakson el 9 de Mzo. de 2019
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 comentarios
Matthew Tyler Jeffries
Matthew Tyler Jeffries el 9 de Mzo. de 2019
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 el 9 de Mzo. de 2019
What's all this? Did you see any of the the compact Answers below?

Iniciar sesión para comentar.

Respuesta aceptada

per isakson
per isakson el 8 de Mzo. de 2019
Editada: per isakson el 8 de Mzo. de 2019
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 comentarios
Matthew Tyler Jeffries
Matthew Tyler Jeffries el 9 de Mzo. de 2019
Editada: per isakson el 9 de Mzo. de 2019
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 el 9 de Mzo. de 2019
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!

Iniciar sesión para comentar.

Más respuestas (2)

Bob Thompson
Bob Thompson el 8 de Mzo. de 2019
Take a look here for ways to write data in variables to text files.
  1 comentario
per isakson
per isakson el 8 de Mzo. de 2019
There is no section on exporting a string array.

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 8 de Mzo. de 2019
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.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by