Convert Cell Array into equation

조회 수: 5 (최근 30일)
ga97cad
ga97cad 2021년 10월 21일
댓글: Stephen23 2021년 10월 21일
Hi everyone!
I have a simple question and I have already been looking for an answer that suits my problem here in the community but haven't found anything yet so here I go:
I am reading a .csv file with the function readtable and it works perfectly fine, but there is one column were an quation like this is written: 19*32 for example in each cell. What i want is Matlab to just calculate that equation for each cell of the array, to just get one number. For example for the equation above it would be 608. Matlab reads this column as a Cell Array and I already tried the cell2mat function, but that didn't help me unfortunetly....
I have attached a file with an exmaple of the column that I want to solve.
Thank you for your help and best regards from germany
Lukas
  댓글 수: 1
Stephen23
Stephen23 2021년 10월 21일
Slightly more robust would be to use STR2NUM:
T = readtable('matlab_example.csv','Delimiter',',');
V = cellfun(@str2num, T.flaeche_li_2)
V = 28×1
540 360 324 720 432 414 810 468 540 450

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

채택된 답변

Voss
Voss 2021년 10월 21일
If C is a cell array containing a MATLAB expression in each cell, then the following code will evaluate the expressions and return a vector of results:
V = cellfun(@(x)eval(x),C);
  댓글 수: 1
ga97cad
ga97cad 2021년 10월 21일
Thank you, Benjamin!! your solution helped me to solve my problem and I marked it as accepted!! best regards, Lukas

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

추가 답변 (1개)

Dave B
Dave B 2021년 10월 21일
I think you could use a combination of cellfun (to run on each cell) and eval (to evaluate the expression):
a=readtable('matlab_example.csv','Delimiter',',');
a.evaluated=cellfun(@eval,a.flaeche_li_2);
head(a)
ans = 8×2 table
flaeche_li_2 evaluated ____________ _________ {'30*18'} 540 {'20*18'} 360 {'18*18'} 324 {'40*18'} 720 {'24*18'} 432 {'23*18'} 414 {'45*18'} 810 {'26*18'} 468
  댓글 수: 1
ga97cad
ga97cad 2021년 10월 21일
Thank you for your quick response!! it worked out and helped me a lot!! best regards, lukas

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by