how to enter struct into new struct ?

2 ビュー (過去 30 日間)
tomer polsky
tomer polsky 2019 年 1 月 24 日
コメント済み: tomer polsky 2019 年 1 月 24 日
hello I am currently reading data from UART using RS232 cable . the thing is that i can read the data how ever for some reaoson i can read only 512 samples . my question is simple is there a way to enter 512 sample each time to a new structer ?
for exmaple the first time i get 512 samples and then put this sample into a new struct calded data1
then the next time i read a new 512 samples it get to 513 to 1024 . this is myt code :
clc
clear all
% Instrument Connection
% Find a serial port object.
obj1 = instrfind('Type', 'serial', 'Port', 'COM7', 'Tag', '');
% Create the serial port object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
obj1 = serial('COM7');
else
fclose(obj1);
obj1 = obj1(1);
end
% Configure instrument object, obj1.
set(obj1, 'Terminator', {'','LF'});
set(obj1, 'Timeout', 1000.0);
set(obj1, 'Terminator', {'','LF'});
set(obj1, 'Terminator', {'','LF'});
% Instrument Connection
% Connect to instrument object, obj1.
fopen(obj1);
% Instrument Configuration and Control
% for i=0:1:2
% Communicating with instrument object, obj1.
data=zeros(512*2,1)
for i=1:2
data1 = fread(obj1, 512, 'uint8');
data(i)=data1
end
% end
  1 件のコメント
Stephen23
Stephen23 2019 年 1 月 24 日
編集済み: Stephen23 2019 年 1 月 24 日
"...and then put this sample into a new struct calded data1 "
Using numbered variables is a sign that you are doing something wrong. Your code will be much simpler and more efficient if you use indexing (such as what Bob Nbob shows in their answer).

サインインしてコメントする。

回答 (1 件)

Bob Thompson
Bob Thompson 2019 年 1 月 24 日
I'm going to assume that data(i) = data1 does not work, because you're trying to put an entire array into a single element of a doubles matrix. You can either replace this by doing a cell matrix for data, or by doing a structure as you suggested.
data{i} = data1 % Cell method (index with curly braces)
instrument(i).data = data1 % structure method (probably want to change variable names
  1 件のコメント
tomer polsky
tomer polsky 2019 年 1 月 24 日
never mind I fixed my code , thank you anyway

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by