Given a long (50-2000 each set in scientific notation ex. +1.394013E-10) string of multiple numbers, how can I convert it to an array quickly.

2 vues (au cours des 30 derniers jours)
Hello, Dumb Intern #40019 here.
I was making a basic app to take configure and take readings from an electroscope (Keithley 6514) through a basic GPIB setup and found that its output is based in strings (makes sense), however, I need to upload the Data into an Array for processing but for the life can't figure out how to transfer the string to an array without using a brute force method like a for loop to cycle through the string that would slow down the recording loop and wanted to see if there was an easier solution I was missing.
Example String: "+1.362134E-10,+1.381140E-10,+1.397078E-10,+1.401982E-10,+1.400756E-10,+1.401981E-10,+1.405048E-10,+1.400143E-10,+1.405661E-10,+1.387271E-10,+1.397078E-10,+1.395853E-10,+1.404435E-10,+1.389107E-10,+1.400756E-10,+1.390334E-10,+1.397080E-10,+1.395239E-10,+1.401369E-10,+1.384205E-10,+1.405049E-10,+1.392173E-10,+1.404437E-10,+1.398304E-10,+1.397692E-10,+1.402597E-10,+1.384205E-10,+1.401982E-10,+1.394622E-10,+1.413019E-10,+1.400756E-10,+1.403823E-10,+1.405048E-10,+1.396464E-10,+1.392173E-10,+1.408728E-10,+1.400755E-10,+1.387268E-10,+1.398306E-10,+1.407500E-10,+1.397076E-10,+1.406889E-10,+1.398918E-10,+1.397690E-10,+1.394013E-10,+1.394627E-10,+1.408725E-10,+1.405659E-10,+1.404434E-10,+1.392788E-10\n"
How im pulling the data ( a lot of this is irrelevant but i figured couldn't hurt to put)
%setup the string to take X readings, but first put it in FAST MODE
%serial_write("SENS:CHAR:NPLC 6",device) #0.33
fprintf("Hello Readin \n")
part = "SENS:CHAR:NPLC ";
NPLC_Num = 60 / app.SampleRate;
fullSTR = part + num2str(NPLC_Num);
%c2 = class(fullSTR)
serial_write(app,fullSTR,device)%0.055
timer = 0.06;
%Sample_Rate = app.SampleRate;
%serial_write("SENS:CHAR:NPLC 0.1",device) #0.011
%serial_write("SYST:ZCH OFF",device) #turn on the zero offset count
%serial_write("DISP:ENAB OFF",device) #turn on the display
%normal junk as non fast mode below
t1 = "TRIG:COUNt ";
%a = number_to_take
full = t1 + num2str(number_to_take);
MatData = [];
%Write all the arming/trigger settings
serial_write(app,"ARM:SOURce IMMediate",device)
serial_write(app,"ARM:COUNt 1",device)
serial_write(app,"TRIG:SOURce IMMediate",device)
serial_write(app,full,device)
serial_write(app,":read?",device)
%need to wait ~166.66ms per reading in slow mode which is the default
count = 1;
%only need to do this if the counter is >100
while(count <= number_to_take)
if count > 100
pause(timer*app.SampleRate)
else
pause(.25)
end
serial_write(app,"++read",device)
TemperData = serial_read(app,device);
MatData = [MatData ,TemperData];
%fprintf(TempData)
count = count + app.SampleRate;
end
end
Here the functions Im reading and writing with.
function serial_write(app,msg,device)
writeline(device,msg)
pause(.01)
end
function output = serial_read(app,device)
pause(.1)
output = '';
while device.NumBytesAvailable > 0
output = output + readline(device) + "\n";
end
end

Réponse acceptée

Stephen23
Stephen23 le 1 Juil 2022
The very efficient approach is to use SSCANF:
str = "+1.362134E-10,+1.381140E-10,+1.397078E-10,+1.401982E-10,+1.400756E-10,+1.401981E-10,+1.405048E-10,+1.400143E-10,+1.405661E-10,+1.387271E-10,+1.397078E-10,+1.395853E-10,+1.404435E-10,+1.389107E-10,+1.400756E-10,+1.390334E-10,+1.397080E-10,+1.395239E-10,+1.401369E-10,+1.384205E-10,+1.405049E-10,+1.392173E-10,+1.404437E-10,+1.398304E-10,+1.397692E-10,+1.402597E-10,+1.384205E-10,+1.401982E-10,+1.394622E-10,+1.413019E-10,+1.400756E-10,+1.403823E-10,+1.405048E-10,+1.396464E-10,+1.392173E-10,+1.408728E-10,+1.400755E-10,+1.387268E-10,+1.398306E-10,+1.407500E-10,+1.397076E-10,+1.406889E-10,+1.398918E-10,+1.397690E-10,+1.394013E-10,+1.394627E-10,+1.408725E-10,+1.405659E-10,+1.404434E-10,+1.392788E-10\n";
vec = sscanf(str,'%f,')
vec = 50×1
1.0e-09 * 0.136213400000000 0.138114000000000 0.139707800000000 0.140198200000000 0.140075600000000 0.140198100000000 0.140504800000000 0.140014300000000 0.140566100000000 0.138727100000000

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by