MATLAB Answers

Older MATLAB script not able to run in 2019 version

17 ビュー (過去 30 日間)
c whyte
c whyte 2019 年 6 月 24 日
回答済み: Steven Lord 2019 年 6 月 24 日
I am trying to run an older MATLAB code and hjaving issues running due to some syntaxual problems. I am not familiar with MATLAB very much and have tried to get this to work.
This code was modified by Daniel-Dane on PJRC.com.
The code comes from http://www-inst.eecs.berkeley.edu/~e.../L12_2_f09.pdf (slide 18 on page 9). It is for taking data from a Teensy 3.6 and doing gamma spectroscopy.
The Teensy board functions as a transport of serial data from its ADC which gets binned by energy in a histogram.
If anyone knows how to get this code to work I would be so greatful. On older versions (2014) of MATLAB this code works flawlessly
%% Data collection %%
% time to run in seconds
t = 60*60;
% Communication port to the Teensy
COMPORT = 'COM8';
% Prepare file with current date and time
strname = ['data/' strjoin(fix(clock), '-') '.txt'];
fid = fopen(strname, 'w');
% Prepare serial communication
s = serial(COMPORT);
try
fopen(s);
catch %#ok<CTCH>
% Failed, properly because the handle was not closed, so we're freeing
% all the handles and trying again.
delete(instrfindall)
s = serial(COMPORT);
fopen(s);
end
% Using try/catch to safely let go of serial handle in case of trouble
try
% Prepare timer
tic
% Flush buffer
if (s.BytesAvailable > 0)
fread(s, s.BytesAvailable);
end
% Data acquisition
while (toc < t)
% Read value from buffer
val = fscanf(s);
% Save value to file
fwrite(fid, val);
end
catch err
% Deinitialize
fclose(s);
delete(s);
fclose(fid);
clear s t
rethrow(err);
end
% Deinitialize
fclose(s);
delete(s);
fclose(fid);
clear s t
%% Data processing %%
% Read from file
assert(exist('strname', 'var')==1, 'voltage_reader error: strname not set.');
if exist(strname, 'file') ~= 2
error(['voltage_reader error: File (strname=' strname ') doesn''t exist.']);
end
fid = fopen(strname, 'r');
try
% Ignore two first lines in case of data collection started in the middle of a transfer
output = textscan(fid, '%f', 'HeaderLines', 2);
output = output{1};
% Ignore last line in case of data collection ended in the middle of a transfer
output(end) = [];
catch err
fclose(fid);
clear fid
rethrow(err);
end
% Get file last modified date minus creation date (=the duration for the experiment)
listing = dir(strname);
fname = listing.name;
fname = fname(1:end-4);
time = (datenum(listing.date) - datenum(fname, 'yyyy-mm-dd-HH-MM-SS')) * 24*60; % in minutes
% Deinitialize
fclose(fid);
clear fid listing fname
% Make histogram (place into bins)
[n, xout] = hist(output, 1000);
% Show data
figure;
bar(xout, n);
xlabel('Channel');
ylabel('Counts');
title(['Running time: ' int2str(time) ' minutes (' int2str(time*60) ' seconds)']);
% Show log(data)
figure;
n = log(n);
bar(xout, n);
xlabel('Channel');
ylabel('log(conts)');
title(['Running time: ' int2str(time) ' minutes (' int2str(time*60) ' seconds)']);
% Deinitialize
clear xout n time
function [dnl,inl,misscodes] = dnl_inl_sin(y)
%DNL_INL_SIN
% dnl and inl ADC output
% input y contains the ADC output
% vector obtained from quantizing a
% sinusoid
% Boris Murmann, Aug 2002
% Bernhard Boser, Sept 2002
% histogram boundaries
minbin=min(y);
maxbin=max(y);
% histogram
[h,x] = hist(y, minbin:maxbin);
% cumulative histogram
ch = cumsum(h);
% transition levels found by:
T = -cos(pi*ch/sum(h));
% linearized histogram
hlin = T(2:end) - T(1:end-1);
% truncate at least first and last
% bin, more if input did not clip ADC
trunc=2;
hlin_trunc = hlin(1+trunc:end-trunc);
% calculate lsb size and dnl
lsb= sum(hlin_trunc) / (length(hlin_trunc));
dnl= [0 hlin_trunc/lsb-1];
misscodes = length(find(dnl<-0.99));
% calculate inl
inl= cumsum(dnl);
figure
subplot(2, 2, 1);
plot(x, h);
xlabel('Channel');
ylabel('Counts');
title('Histogram');
subplot(2, 2, 2);
plot(1:length(dnl), dnl);
xlabel('Channel');
ylabel('DNL [LSB]');
title(['DNL = +' num2str(max(dnl),2) ' / ' num2str(min(dnl),2) ' LSB. No. of missing codes (DNL < -0.99): ' int2str(misscodes)]);
subplot(2, 2, 3);
plot(x, log(h));
xlabel('Channel');
ylabel('log(counts)');
title('Log-histogram');
subplot(2, 2, 4);
plot(1:length(inl), inl);
xlabel('Channel');
ylabel('INL [LSB]');
title(['INL = +' num2str(max(inl),2) ' / ' num2str(min(inl),2) ' LSB.']);

  4 件のコメント

表示 1 件の古いコメント
Himanshu Rai
Himanshu Rai 2019 年 6 月 24 日
Can you specify what syntaxual problems you are getting ?
Alex Mcaulley
Alex Mcaulley 2019 年 6 月 24 日
What is the complete error text are you getting? What is the Matlab release are you using? This code works fine for me (R2016b)
c whyte
c whyte 2019 年 6 月 24 日
For me it is (R2019a)
The problem stems from: Error using strjoin (line 53)
First input must be a string array or cell array of character vectors.
Also function [dnl,inl,misscodes] = dnl_inl_sin(y) Doesn't work either. dnl_inl_sin(y) is unused... ??? At a complete loss.
It doesn't like strjoin for some odd reason.

サインイン to comment.

回答 (4 件)

Shwetank Shrey
Shwetank Shrey 2019 年 6 月 24 日
Change
strname = ['data/' strjoin(fix(clock), '-') '.txt'];
to
strname = ['data/' char(strjoin(string(fix(clock)), '-')) '.txt'];

  0 件のコメント

サインイン to comment.


c whyte
c whyte 2019 年 6 月 24 日
I am using MATLAB R2019a and I am getting several errors on
Error using strjoin (line 53) First input must be a string array or cell array of character vectors.
Not sure what's going on here.

  0 件のコメント

サインイン to comment.


c whyte
c whyte 2019 年 6 月 24 日
strname = ['data/' strjoin(fix(clock)
, '-') '.txt'];
fid = fopen(strname, 'w');

  0 件のコメント

サインイン to comment.


Steven Lord
Steven Lord 2019 年 6 月 24 日
The output of fix(clock) is neither a string array nor a cell array each element of which is a string or a char vector. Therefore you can't strjoin those elements together.
Instead I recommend using the datetime function and specifying a 'Format' that matches what you want. See the documentation for datetime for allowed values for the datetime Format option.
D1 = datetime('now')
C1 = char(D1)
S1 = string(D1)
F1 = "data/" + S1 + ".txt"
D2 = datetime('now', 'Format', 'yyy-MM-dd-HH-mm-ss')
C2 = char(D2)
S2 = string(D2)
F2 = "data/" + S2 + ".txt"

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。


Translated by