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

dpb
dpb el 8 de Mzo. de 2019
We'd have to see how you created the file. A small example will serve just as well as a large one....
Matthew Tyler Jeffries
Matthew Tyler Jeffries el 8 de Mzo. de 2019
string.PNG
This is part of the string array that is produced. It dimensions are 1923X3
Bob Thompson
Bob Thompson el 8 de Mzo. de 2019
What commands did you use to create the actual text file?
dpb
dpb el 8 de Mzo. de 2019
Even better, attach a sample small(ish) sample code/data...as text, not image so somebody can do something with it besides just look...
Matthew Tyler Jeffries
Matthew Tyler Jeffries el 8 de Mzo. de 2019
The string is produced as a workspace variable in matlab, and is currently not a text file. I'm not sure how to convert it to a text file (could I open the string in Notepad++ if it were a text file?).
Thank you for your help.
dpb
dpb el 8 de Mzo. de 2019
Again, it's much easier for folks to have actual code/data to work with rather than generalities and definitely better than images!
There are a number of ways to write string data to a file, but just what, specifically, is dependent upon how you actually created the data.
Matlab has multiple choices; it could be a char() array, a cellstr() array or the new(ish) string() array. Which it is, and how to write it, is dependent upon that.
The image above makes it appear it may be the new string() class; if so, then fprintf is string aware.
See
doc fprintf % for information examples
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

0 votos

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

Walter Roberson
Walter Roberson el 9 de Mzo. de 2019
Per, I tracked the cause of the number of elements mismatch for writetable(table(str)), and I have reported it to Mathworks along with a suggested fix.
Workaround:
writetable( array2table(str), 'str.csv' )
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

0 votos

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

0 votos

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

Etiquetas

Preguntada:

el 8 de Mzo. de 2019

Editada:

el 9 de Mzo. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by