Fgetl, Fscanf, Fgets, Fprintf Execution Time
4 views (last 30 days)
Show older comments
Hi, I have created a serial communication for send and receive data. However, these function allows sending data to the serial port and retrieving the data from it. I understood it need some times (few seconds) for the function to execute. However, when i loop it. The time will be increase exponentially. I believe it cause hanging to my code. Can someone have better suggestion to replace fprintf for sending and fgetl for receiving binary bits to allow faster execution time? Many thanks in advance
Here is the coding for send and receive: For Send:
%Open the file
fid=fopen('XXX.mp3','rb'); % Open a file inside MATLAB any music file.
A=fread(fid,'uint16'); % Display the data in uint16
B=dec2bin(A); % Change decimal (0-65535) into 16 binary bits and store in matrix (Mx1) matrix
F=B'; % Transpose it so it will not in matrix format
[M N]=size(A); %Determine the size
looptimes=fix(M/5000);
last=rem(M, 5000);
send_count=1;
loop_count=0;
C=[];
for i=1+5000*(send_count-1):5000*send_count %To send data 5kB
C=[C F((-15+16*(i)):16*(i))]; %16bits everytime loop and send
end
fprintf(s,C) %Send to the serial port
send_count=1+send_count;
For receive:
file=[];
received_count=1;
loop_count=0;
while strcmp(s.PinStatus.ClearToSend,'on')
s.RequestToSend='on';
received=fgetl(s); %To receive bits from serial port
file=[file received]; %Concatenate back
received_count=received_count+1;
size_byte=fix(size(file)/32); % The size of the file
M=size_byte(2);
Video_bin=zeros(M,1);
for j=1:M % To arrange back the file
b=16*(j);
a=-15+b;
Video_bin= [Video_bin;file(a:b)];
end
Video_dec=bin2dec(Video_bin); % From binary to decimal
fid1=fopen (Save_file, 'wb'); % Form back the original file
count=fwrite(fid1, Video_dec, 'uint16');
Accepted Answer
Oleg Komarov
on 24 Apr 2011
Use the profiler to identify the bottlenecks and did you preallocate (if possible) when importing?
EDIT
About preallocation and taking as an example the code you posted below:
% Example inputs
A1 = [];
M = 6000;
B = randi([0,1],1,16*M);
% Manual reshape WITHOUT preallocation
tic
for j = 1:M
b = 16*(j);
a = -15+b;
A1 = [A1; B(a:b)];
end
toc
% Manual reshape WITH preallocation
tic
A2 = zeros(M,16);
for j = 1:M
b = 16*(j);
a = -15+b;
A2(j,:) = B(a:b);
end
toc
% Reshape
tic
A3 = reshape(B,16,M).';
toc
A1: Elapsed time is 1.708695 seconds.
A2: Elapsed time is 0.014439 seconds.
A3: Elapsed time is 0.001066 seconds.
23 Comments
Oleg Komarov
on 26 Apr 2011
This is getting messy. Please edit your original question posting the formatted codeas you're using it now.
More Answers (1)
Walter Roberson
on 24 Apr 2011
Serial port delays are usually in the fraction of seconds rather than in seconds, unless you are talking about the time to transmit a large batch of information.
I can think of at least two different meanings for talking about "looping" the program, and neither of them would result in an exponential increase in the execution time.
I have the suspicion that you should be switching to use the BytesAvailableFcn serial callback, and possibly switching terminator mode and possibly terminator character.
Does data get lost when you allow the transmission to proceed at full speed? If it does then you should probably be using hardware flow control.
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!