Clear Filters
Clear Filters

timetable을 input 신호로 사용할 때 time column의 문자열 처리

2 views (last 30 days)
eblee
eblee on 1 Feb 2024
Commented: Angelo Yeo on 7 Feb 2024
여러 행을 가지는 timetable을 엑셀로 저장하여 input 신호로 사용하려고 합니다.
From Spreadsheet로 엑셀파일의 값들을 읽어올 때 첫번째 time행에 '초'시간 단위로 인해 'First column (time column) must contain numeric values.'이라는 오류가 발생합니다.
시간을 읽어올 때 문자열을 제외하고 숫자만 읽어올 수는 없을까요?

Accepted Answer

Angelo Yeo
Angelo Yeo on 1 Feb 2024
이런 경우 파일 전처리를 수행하는게 좋을 것 같습니다. 전처리를 수행하셔서 "초" 라는 글자를 제외한 숫자만 받아서 다시 저장할 수 있습니다. 아래의 스크립트를 참고해주십시오.
RowTimes = seconds(1:5)';
TestCase = timetable(RowTimes,[98;97.5;97.9;98.1;97.9],[120;111;119;117;116],...
'VariableNames',{'Reading1','Reading2'});
writetimetable(TestCase, "test.xlsx");
clear RowTimes TestCase
%% When reading table
myTbl = readtable('test.xlsx')
myTbl = 5×3 table
RowTimes Reading1 Reading2 _________ ________ ________ {'1 sec'} 98 120 {'2 sec'} 97.5 111 {'3 sec'} 97.9 119 {'4 sec'} 98.1 117 {'5 sec'} 97.9 116
RowTimesPruned = extractBefore(string(myTbl.RowTimes), "sec"); % To remove "sec" but to get only numbers
myTbl.RowTimes = double(RowTimesPruned)
myTbl = 5×3 table
RowTimes Reading1 Reading2 ________ ________ ________ 1 98 120 2 97.5 111 3 97.9 119 4 98.1 117 5 97.9 116
  2 Comments
eblee
eblee on 7 Feb 2024
답변 감사합니다.
그런데 답변 주신 내용을 참고하여 모델을 실행한 결과 "Cannot pass a mxArray to 'string'."이라는 경고가 발생합니다.
현재 mxArray를 string형으로 바꾸는 방법을 찾지 못하고 있는데 MATLAB Function내에서 이러한 형변환을 수행할 수 있는 방법이 있을까요?
Angelo Yeo
Angelo Yeo on 7 Feb 2024
@은비: 저는 MATLAB에서 전처리 하는 방법에 대해서 말씀드린 것인데요. 말씀하신 에러 메시지는 전처리 과정까지도 코드 생성하여 진행하려고 하시는 것으로 보입니다.
이는 원래 제 답변 의도와는 차이가 있는 진행 방식이기 때문에 (그리고 그런 배경은 질문에도 보이지 않습니다), 말씀하신 에러를 얻기 위한 파일과 전체적인 재현 방법을 공유해주셔야 구체적으로 답변드릴 수 있습니다. 구체적인 자료와 함께 질문을 새롭게 만들어주시는게 좋지 않을까 생각합니다.

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!