how to divide three element array string and assign independent variable matlab?

hello recently I have a network analyzer equipment and I have connected it to be able to make an application in matlab, currently I have a problem when I request the values of the frequency marker that I assign in the equipment it returns a three-element string vector, I want to separate each element and assign it an independent variable to be able to manipulate that data.
fprintf(obj1,'MARKOFF');
fprintf(obj1,'MARKUNCO');
pause(5);
fprintf(obj1,'MARKDISC');
%%fprintf(obj1, "MARK1;1.5 GHZ" );
fprintf(obj1, "MARK1;" );
fprintf(obj1, "SEAMAX;" );
fprintf(obj1, "OUTPMARK;" );
%data2=scanstr(obj1,'%f');
%disp(data2);
%mkr =fscanf(obj1,'%s');
%disp(mkr);
data2=scanstr(obj1);
disp(data2);
the output display is :
[ -0.4198]
[3.9630e+18]
[ 323687500]

4 Comments

There is a simple way to do exactly what you want.
data2(1)
data2(2)
data2(3)
As you see, you can simply access any element of that vector. Creating new variables on the fly is a really bad idea though.
Thank for you support.
I found an example in hpib language compiled in c but the truth already proved it by changing some commands in matlab but it gives errors.
main ()
{
float reply; /* Response to OPC? commands */
char read[80]; /* String for data returned */
float marker[3]; /* Marker results array */
system ( "CLS" ); /* Clear screen display */
initialize( &ISC, &NWA ); /* Initialize the HP-IB & the NWA */
output( NWA, "OPC?;PRES;" ); /* Preset the analyzer and wait */
enter( NWA, &reply ); /* Read the 1 when complete */
output( NWA, "OPC?;SING;" ); /* Single sweep mode and wait */
enter( NWA, &reply ); /* Read the 1 when complete */
output( NWA, "MARK1;" ); /* Turn on marker 1 */
output( NWA, "SEAMAX;;" ); /* Find the maximum */
output( NWA, "OUTPMARK;" ); /* Request the current value */
/* Read the markers as a three element array */
ibrd( NWA, read, 80 ); /* Read the string and convert */
sscanf( read, "%f, %f, %f", &marker[0], &marker[1], &marker[2] );
/* Print out the three possible values of the marker */
printf( "Value1\tValue2\t\tStimulus (Hz)\n" );
printf( "%2.3f\t%e\t%3.6e", marker[0], marker[1], marker[2] );
ibloc( NWA ); /* Release the analyzer to local */
ibonl( NWA, 0 ); /* Place the device offline */
exit( 0 );
}
Yes, but that is c, not MATLAB. Do you really expect that what makes sense in one language will apply in another? By your logic, merely by my understanding English, I should also be able to speak and write flawless Chinese, French, Latin and Farsi, not to mention any of hundreds of other languages.

Sign in to comment.

 Accepted Answer

So it returns a string type matrix?, maybe if you try to convert it to type double you could be able to manipulate it more freely. But anyway, if you want to separate each string and assign it to different variables, you could use something like:
variable_type_1=[];
variable_type_1=[variable_type_1;output_display(1)]
% OR a vertcat command (which does the same thing)
variable_type_1=[];
variable_type_1=vertcat(variable_type_1,output_display(1))
and run this every time you have an output, your data will accumulate in each variable.
for the other variables is the same thing, you just change the name of variable and the element of the output like this:
variable_type_2=[];
variable_type_2=[variable_type_2;output_display(2)]
and so on.
edit: I should mention that if you are dealing with strings, maybe convert the variables to strings first like this:
variable_type_1=strings([]);
Does that help?

1 Comment

if a wish to convert variable_type_1=[]; and variable_type_2=[]; to number ,
How can I convert the result to a number?

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!