How do I save data from a fprintf loop?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Hi,
I have this bit of code, that takes an input as string (usually something like 'hello'), converts it to its binary equiv and then encodes it, then outputs it using the following:
fprintf(['\nDecoded Message = %2d\n Coded: Error rate = %1.2f, ' ...
'Number of errors = %d\n'], ...
Bin(ii),errStats(1),errStats(2));
But I'm unsure how to extract the data generated from this and save it in an array, so that I can use it elsewhere.
For context here is the rest of the code it runs off:
for ii = 1:1:N
dpskdemod = comm.DPSKDemodulator(8,pi/4);
dpskdemod2 = comm.DPSKDemodulator(M);
for counter = 1:numframes
data = randi([0 1],cfgLDPCEnc.NumInformationBits,1,'int8');
% Transmit and receive with LDPC coding
encodedData = ldpcEncode(data,cfgLDPCEnc);
reshape(encodedData, 1, []);
modSignal = dpskmod(encodedData);
recievedSignal = awgn(modSignal,1);
demodSignal = dpskdemod(recievedSignal);
recievedBits = ldpcDecode(demodSignal,cfgLDPCDec,maxnumiter);
errStats = ber(data,recievedBits);
% Transmit and receive with no LDPC coding
noCoding = dpskmod2(data);
rxNoCoding = awgn(noCoding,Bin(ii));
rxBitsNoCoding = dpskdemod2(rxNoCoding);
errStatsNoCoding = ber2(data,int8(rxBitsNoCoding));
end
Thank you,
Connor
채택된 답변
dpb
2022년 8월 25일
msgs=string(N,1);
for ii=1:N
....
msgs(ii)=compose(['\nDecoded Message = %2d\n Coded: Error rate = %1.2f, ' ...
'Number of errors = %d\n'], Bin(ii),errStats(1),errStats(2));
fprintf(msgs(ii))
end
to save the actual text itself; not clear just what you intend by " how to extract the data generated from this ".
If it's just the data itself, then a struct array might be the ticket...
for ii=1:N
....
fprintf(['\nDecoded Message = %2d\n Coded: Error rate = %1.2f, ' ...
'Number of errors = %d\n'], Bin(ii),errStats(1),errStats(2));
msgs(ii).Bin=Bin(ii);
msgs(ii).Err=errStats;
end
to save the data itself as a struct array.
댓글 수: 8
Connor Baker
2022년 8월 25일
편집: dpb
2022년 8월 25일
Hi! Thank you for answering.
Sorry, I should've made it more clear.
When I say extract, what I mean is that the code runs a loop which spits out a bunch of numbers, which I want to be able to access so I can plot it using stairs (As it should be 1's and 0's).
In its current form, it doesn't seem to save it, atleast in the format I want.
I tried your two lots of code and it doesn't seem to let me access it in that way. Maybe I'm doing something wrong.
I'll provide the whole section of code as it might provide a better clarity of what I want. When you run it, the output has a 'decoded message' in the command window, its the numbers that follow after that (1's and/or 0's) as opposed to the error rate. I hope that makes sense.
%Clearing the workspace and command window
clear;
clc;
close all;
% Getting input from user
%A = input("what is your message? ", 's');
A='hello'; % input a string so will run -- dpb
% Convert input into Binary, without array form
Bin = reshape(dec2bin(A, 8).'-'0',1,[]);
N = length(Bin);
Binx = reshape(Bin,N,[]);
X = ['Your message "', A, '" has now been converted to binary: '];
disp(X);
Your message "hello" has now been converted to binary:
fprintf('%d',Bin); % Shows the binary representation of the message
0110100001100101011011000110110001101111
% Setting Parameters for binary to digital
% For the message
Fs = 1e4; % Sampling Frequency (12MHz)
Fc = 48000;
Ts = 1/Fs; % Sampling Period
f = 10^3; % Bit rate
T = 1/f; % Bit period
t1 = 0:T:(N-1)*T; % Time period
t2 = 0:Ts:T-Ts;
Pc1 = pi; % Carrier phase for binary input '1' - 180° Phase
Pc2 = 0; % Carrier phase for binary input '0'
Ac = 1; % Carrier amplitude for binary input
h = 1; % Signal fading
w = 0; % Noise
% Displaying the signal using stairs
figure('Name','DPSK Modulation and Demodulation','NumberTitle','off');
subplot(3,1,1);
stairs(t1, Bin,'b','LineWidth',2);
grid on;
axis([0 (N-1)*T -1 2]);
xlabel('Time(Sec)');
ylabel('Amplitude');
title('Digital Message Signal');

% ----- LDPC Code Channel Encoder (Error Correction Scheme) -------
% Parameters for Encoding/Decoding
blockSize = length(Bin);
pcmatrix = ldpcQuasiCyclicMatrix(blockSize,Bin);
cfgLDPCEnc = ldpcEncoderConfig(pcmatrix);
cfgLDPCDec = ldpcDecoderConfig(pcmatrix);
M = 8; % Modulation order DPSK has powers of 2
numframes = 10;
maxnumiter = 10;
ber = comm.ErrorRate;
ber2 = comm.ErrorRate;
dpskmod = comm.DPSKModulator(8,pi/4);
dpskmod2 = comm.DPSKModulator(M);
for ii = 1:1:N
dpskdemod = comm.DPSKDemodulator(8,pi/4);
dpskdemod2 = comm.DPSKDemodulator(M);
for counter = 1:numframes
data = randi([0 1],cfgLDPCEnc.NumInformationBits,1,'int8');
% Transmit and receive with LDPC coding
encodedData = ldpcEncode(data,cfgLDPCEnc);
reshape(encodedData, 1, []);
modSignal = dpskmod(encodedData);
recievedSignal = awgn(modSignal,1);
demodSignal = dpskdemod(recievedSignal);
recievedBits = ldpcDecode(demodSignal,cfgLDPCDec,maxnumiter);
errStats = ber(data,recievedBits);
% Transmit and receive with no LDPC coding
noCoding = dpskmod2(data);
rxNoCoding = awgn(noCoding,Bin(ii));
rxBitsNoCoding = dpskdemod2(rxNoCoding);
errStatsNoCoding = ber2(data,int8(rxBitsNoCoding));
end
fprintf(['\nDecoded Message = %2d\n Coded: Error rate = %1.2f, ' ...
'Number of errors = %d\n'], ...
Bin(ii),errStats(1),errStats(2));
fprintf(['Noncoded: Error rate = %1.2f, ' ...
'Number of errors = %d\n'], ...
errStatsNoCoding(1),errStatsNoCoding(2))
end
Decoded Message = 0
Coded: Error rate = 0.55, Number of errors = 8609
Noncoded: Error rate = 0.71, Number of errors = 11052
Decoded Message = 1
Coded: Error rate = 0.55, Number of errors = 17098
Noncoded: Error rate = 0.70, Number of errors = 21713
Decoded Message = 1
Coded: Error rate = 0.55, Number of errors = 25730
Noncoded: Error rate = 0.69, Number of errors = 32304
Decoded Message = 0
Coded: Error rate = 0.55, Number of errors = 34401
Noncoded: Error rate = 0.70, Number of errors = 43435
Decoded Message = 1
Coded: Error rate = 0.55, Number of errors = 42967
Noncoded: Error rate = 0.69, Number of errors = 54078
Decoded Message = 0
Coded: Error rate = 0.55, Number of errors = 51506
Noncoded: Error rate = 0.70, Number of errors = 65300
Decoded Message = 0
Coded: Error rate = 0.55, Number of errors = 60038
Noncoded: Error rate = 0.70, Number of errors = 76558
Decoded Message = 0
Coded: Error rate = 0.55, Number of errors = 68572
Noncoded: Error rate = 0.70, Number of errors = 87712
Decoded Message = 0
Coded: Error rate = 0.55, Number of errors = 77209
Noncoded: Error rate = 0.70, Number of errors = 98914
Decoded Message = 1
Coded: Error rate = 0.55, Number of errors = 85755
Noncoded: Error rate = 0.70, Number of errors = 109516
Decoded Message = 1
Coded: Error rate = 0.55, Number of errors = 94467
Noncoded: Error rate = 0.70, Number of errors = 120158
Decoded Message = 0
Coded: Error rate = 0.55, Number of errors = 102966
Noncoded: Error rate = 0.70, Number of errors = 131324
Decoded Message = 0
Coded: Error rate = 0.55, Number of errors = 111503
Noncoded: Error rate = 0.70, Number of errors = 142401
Decoded Message = 1
Coded: Error rate = 0.55, Number of errors = 120044
Noncoded: Error rate = 0.70, Number of errors = 152998
Decoded Message = 0
Coded: Error rate = 0.55, Number of errors = 128649
Noncoded: Error rate = 0.70, Number of errors = 164176
Decoded Message = 1
Coded: Error rate = 0.55, Number of errors = 137099
Noncoded: Error rate = 0.70, Number of errors = 174819
Decoded Message = 0
Coded: Error rate = 0.55, Number of errors = 145716
Noncoded: Error rate = 0.70, Number of errors = 186037
Decoded Message = 1
Coded: Error rate = 0.55, Number of errors = 154317
Noncoded: Error rate = 0.70, Number of errors = 196639
Decoded Message = 1
Coded: Error rate = 0.55, Number of errors = 162867
Noncoded: Error rate = 0.70, Number of errors = 207410
Decoded Message = 0
Coded: Error rate = 0.55, Number of errors = 171538
Noncoded: Error rate = 0.70, Number of errors = 218533
Decoded Message = 1
Coded: Error rate = 0.55, Number of errors = 180069
Noncoded: Error rate = 0.70, Number of errors = 229238
Decoded Message = 1
Coded: Error rate = 0.55, Number of errors = 188578
Noncoded: Error rate = 0.70, Number of errors = 239827
Decoded Message = 0
Coded: Error rate = 0.55, Number of errors = 197126
Noncoded: Error rate = 0.70, Number of errors = 250974
Decoded Message = 0
Coded: Error rate = 0.55, Number of errors = 205654
Noncoded: Error rate = 0.70, Number of errors = 262155
Decoded Message = 0
Coded: Error rate = 0.55, Number of errors = 214226
Noncoded: Error rate = 0.70, Number of errors = 273301
Decoded Message = 1
Coded: Error rate = 0.55, Number of errors = 222729
Noncoded: Error rate = 0.70, Number of errors = 283978
Decoded Message = 1
Coded: Error rate = 0.55, Number of errors = 231380
Noncoded: Error rate = 0.70, Number of errors = 294738
Decoded Message = 0
Coded: Error rate = 0.55, Number of errors = 239985
Noncoded: Error rate = 0.70, Number of errors = 305956
Decoded Message = 1
Coded: Error rate = 0.55, Number of errors = 248585
Noncoded: Error rate = 0.70, Number of errors = 316634
Decoded Message = 1
Coded: Error rate = 0.55, Number of errors = 257114
Noncoded: Error rate = 0.70, Number of errors = 327405
Decoded Message = 0
Coded: Error rate = 0.55, Number of errors = 265708
Noncoded: Error rate = 0.70, Number of errors = 338513
Decoded Message = 0
Coded: Error rate = 0.55, Number of errors = 274195
Noncoded: Error rate = 0.70, Number of errors = 349673
Decoded Message = 0
Coded: Error rate = 0.55, Number of errors = 282761
Noncoded: Error rate = 0.70, Number of errors = 360777
Decoded Message = 1
Coded: Error rate = 0.55, Number of errors = 291331
Noncoded: Error rate = 0.70, Number of errors = 371420
Decoded Message = 1
Coded: Error rate = 0.55, Number of errors = 299909
Noncoded: Error rate = 0.70, Number of errors = 382093
Decoded Message = 0
Coded: Error rate = 0.55, Number of errors = 308475
Noncoded: Error rate = 0.70, Number of errors = 393305
Decoded Message = 1
Coded: Error rate = 0.55, Number of errors = 317019
Noncoded: Error rate = 0.70, Number of errors = 403883
Decoded Message = 1
Coded: Error rate = 0.55, Number of errors = 325610
Noncoded: Error rate = 0.70, Number of errors = 414512
Decoded Message = 1
Coded: Error rate = 0.55, Number of errors = 334104
Noncoded: Error rate = 0.70, Number of errors = 425282
Decoded Message = 1
Coded: Error rate = 0.55, Number of errors = 342675
Noncoded: Error rate = 0.70, Number of errors = 435911
dpb
2022년 8월 25일
OK, so I edited the code so it would run -- which numbers are you trying to save, specifically?
dpb
2022년 8월 25일
Well, S-S, I showed him how to save the fprintf data to do whatever with and that didn't satisfy...I don't yet know what it is that he's wanting saved...certainly writing to a file is another way, writing the saved array w/ writearray would do the same w/o the need for explicitly opening/closing a file handle.
Connor Baker
2022년 8월 25일
편집: Connor Baker
2022년 8월 25일
I'm trying to save the 1's and 0's that come after the 'Decoded Message'. As that is its own variable, I thought I could grab it like so:
So maybe it would look like
0
1
0
1
0
0
0
1
or 0101001....etc
I do apologise for any confusion, MATLAB is fairly new for me, so I'm trying to learn as much as I can. Thank you both in advance
dpb
2022년 8월 26일
You've already got it -- it's Bin and is an array already -- that's what was confusing me is that you already have what that is; you're displaying it one array element at a time is all, instead of altogether as you did early on when you plotted it (as the string of 0 and 1).
Ahhh yes! Of course!!
Sorry!!! I'm not sure what was going on. I got very confused with what I was doinh.
I do apoligise for the confusion, and thank you very much for answering and trying to work through my mess.
Much appreciated :)
dpb
2022년 8월 26일
Ah...thanks for the clarification. It's easy-enough to lose sight of the forest for the trees--been there, done that, many scars to show for having done...
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 AI for Wireless에 대해 자세히 알아보기
제품
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
