필터 지우기
필터 지우기

How to access a struct through a field name.

조회 수: 22 (최근 30일)
Allan Munro
Allan Munro 2020년 11월 26일
댓글: Image Analyst 2020년 11월 30일
I am having difficulty figuring out how to accessa struct by field name. I have only been able to access it by row number. Can someone help me figure this out?
I am creating a struct with the feilds for differnt stocks. How do I write a command to access a field by matching the stock name?
For example:
ProfitCheck = PortfolioCheck(1).marketPrice > PortfolioCheck(1).averageCost;
I can get it to work with row numbers, but instead I want it to look for a stock name?
I have a list of stocks, 'symbols', and want to have the function look through the struct for a matching field name 'PortfolioCheck.symbol = AAPL then return the 'marketPrice' and 'avererageCost'
If you can point me in the right direction that would be wonderful.
Thanks
Allan
% Connects to get the data and creates a struct 'PortfolioCheck'
PortfolioCheck = IBMatlab('action','portfolio');pause(2)
1×4 struct array with fields:
symbol
localSymbol
exchange
secType
currency
right
expiry
strike
position
marketValue
marketPrice
averageCost
realizedPnL
unrealizedPnL
contract
  댓글 수: 3
Allan Munro
Allan Munro 2020년 11월 26일
Thanks! I'm new to matlab so it is a lot to digest.
Image Analyst
Image Analyst 2020년 11월 26일
But you're a smart guy so I'm assuming you got my answer below to work. If not, attach your variable in a .mat file with the paper clip icon.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2020년 11월 26일
It shouldn't be a problem with what you've shown since every structure in the structure array has a unique value for the symbol field. If you have a situation where the same symbol could appear in multiple strcutures in the array, I suggest you look into ismember() to pull out only those structures in the array where the symbol field has the symbol you're looking to extract. Something like
[ia, ib] = ismember({PortfolioCheck.symbol}, 'BNS');
If that doesn't work, try getting rid of the braces or swapping the order of the input arguments until you get the one that works.
  댓글 수: 2
Allan Munro
Allan Munro 2020년 11월 30일
Hi Image Analyst,
Maybe I am not getting this right in my head...
I have a list of stocks in another list and I want them to be checked against this data. I have the field 'StockSymbol' and these stocks:
StockSymbol
BNS
RY
AAPL
is there a way to check the array 'PortfolioCheck', and the field 'Symbol" to check the array for each symbol i have in the stock list?
Check each 'StockSymbol' on the construct PortfolioCheck.symbol and return the fields 'averageCost' and 'marketPrice' for each.
What would be the syntax to do that? I'm getting stuck in a loop here.
thanks
Allan
Image Analyst
Image Analyst 2020년 11월 30일
Make it easy for us to help you. Can you attach PortfolioCheck in a .mat file:
save('answers.mat', 'PortfolioCheck');
then use the paper clip icon to attach it.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by