Function file doesn`t work
1 回表示 (過去 30 日間)
古いコメントを表示
Hello everyone,
I have three files: a txt. notebook with data, an .m-function file that should read it und the final .m-file that should put out a Bode-Diagramm.
M=readLTspice('Lab5_R_100_MOhm.txt', 'Nyquist')
f=M(:,1);
A=M(:,2);
Ph=M(:,3);
figure(1)
subplot(2,1,1);
plot(f, A , "b");
set(gca, 'XScale', 'log');
subplot(2,1,2);
plot(f, Ph, "g");
set(gca, 'XScale', 'log');
function [M]=readLTspice(fileName, selector)
% MATLAB function to read files exported from LTspice
% M = readLTspice(fileName, selector)
% fileName - name of file to be read as string (including .txt)
% selector - type of simulation (Time, Bode or Nyquist)
% data in time or cartesian form
if selector=="Time" | selector=="time" | selector=="Nyquist" | selector=="nyquist"
tempFileName = strcat(erase(fileName, '.txt'), '_Temp.txt');
textOriginal = readTFile(fileName);
textKorrigiert = regexprep(textOriginal(3:end), '\t', ',');
writeTFile(tempFileName,textKorrigiert);
M = importdata(tempFileName);
delete(tempFileName);
% data in polar form
elseif selector=="Bode" | selector=="bode"
tempFileName = strcat(erase(fileName, '.txt'), '_Temp.txt');
textOriginal = readTFile(fileName);
textKorrigiert = regexprep(textOriginal(3:end), '\t', ',');
textKorrigiert = regexprep(textKorrigiert, '°', '');
textKorrigiert = regexprep(textKorrigiert, 'dB', '');
textKorrigiert = regexprep(textKorrigiert, '(', '');
textKorrigiert = regexprep(textKorrigiert, ')', '');
writeTFile(tempFileName,textKorrigiert);
M = importdata(tempFileName);
delete(tempFileName);
% wrong call
else
error("Argument für Format der exportierten Daten unzulässig!" + char(10) + "Bitte überprüfen Sie, wie die Daten aus LTspice exportiert wurden" + char(10) + "und nutzen eines der folgenden Argumente: [Time], [Bode], [Nyquist]." + char(10) + char(10) + "Der LTspice-Leitfaden im ISIS-Kurs gibt genauere Hinweise zur Verwendung von [readLTspice]." + char(10));
end
end
function T=readTFile(fileName)
textOriginal = '';
fid=fopen(fileName);
tline = fgetl(fid);
while ischar(tline)
textOriginal = textOriginal+"\n"+tline;
tline = fgetl(fid);
end
fclose(fid);
T = splitlines(compose(textOriginal));
end
function writeTFile(fileName,fileText)
fileID = fopen(fileName,'w');
fprintf(fileID,'%s\n',fileText);
fclose(fileID);
end
However, something always goes wrong:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1732593/image.png)
What could I do? Thank you.
0 件のコメント
回答 (1 件)
Voss
2024 年 7 月 11 日
T = readtable('Lab5_R_100_MOhm.txt','VariableNamingRule','preserve')
f = T.(1);
A = T.(2);
Ph = T.(3);
figure
tiledlayout(2,1)
nexttile()
semilogx(f,10*log10(-A))
ylabel('Magnitude')
nexttile()
semilogx(f,Ph)
xlabel('Frequency')
ylabel('Phase')
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Get Started with MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!