필터 지우기
필터 지우기

How to save a text file with number and text information?

조회 수: 3 (최근 30일)
Guilherme Weber Sampaio de Melo
댓글: Voss 2024년 3월 2일
Hello, I have on my MATLAB code some variables containing values and filenames. Ex: lat1=1.2; lat2=3.2; long1=-44; long2=-34; grd=‘map.grd’; fault=‘fault.txt’; I would like to save one file in any output text format to be read by another code with a Shell code. Ex: file.txt containing all informations inside (1.2 3.2 -44 -34 map.grd fault.txt). I tried to use the writetable function as I have used before but it just work to values, it shows error when I tried to include the filenames (e.g map.grd). Does someone have any suggestion? Thanks in advance.

채택된 답변

Voss
Voss 2024년 3월 1일
Put the filenames in cell arrays.
Example:
lat1=1.2; lat2=3.2; long1=-44; long2=-34; grd='map.grd'; fault='fault.txt';
% first, I generate the error you might have gotten:
try
T = table(lat1,lat2,long1,long2,grd,fault) % doesn't work
catch e
disp(e.message);
end
Invalid parameter name: map.grd.
% now, I put grd and fault in cell arrays:
T = table(lat1,lat2,long1,long2,{grd},{fault}) % works
T = 1×6 table
lat1 lat2 long1 long2 Var5 Var6 ____ ____ _____ _____ ___________ _____________ 1.2 3.2 -44 -34 {'map.grd'} {'fault.txt'}
% write the table to file:
writetable(T,'file.txt')
% check the contents of the txt file:
type file.txt
lat1,lat2,long1,long2,Var5,Var6 1.2,3.2,-44,-34,map.grd,fault.txt
  댓글 수: 5
Guilherme Weber Sampaio de Melo
Yes, it worked well to continue the following steps on my code. Thank you very much!
Voss
Voss 2024년 3월 2일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by