I am currently trying to make a simple code named subjex.dat
The goal is to create a 5x2 matrix with these values that will pop up
5.3 a
2.2 b
3.3 a
4.4 a
1.1 b
What i have so far is
a = 'a';
b = 'b';
matrix1 = [5.3, 2.2, 3.3, 4.4, 1.1]'
matrix2 = [a,b,a,a,b]'
matrix3 = [matrix1,matrix2]
How do I get the numbers to show? Right now, all this I see is "5x2 char array" and then the letters

 채택된 답변

Walter Roberson
Walter Roberson 2021년 12월 10일

0 개 추천

a = 'a';
b = 'b';
matrix1 = [5.3, 2.2, 3.3, 4.4, 1.1]'
matrix1 = 5×1
5.3000 2.2000 3.3000 4.4000 1.1000
matrix2 = [a,b,a,a,b]'
matrix2 = 5×1 char array
'a' 'b' 'a' 'a' 'b'
matrix3 = table(matrix1,matrix2)
matrix3 = 5×2 table
matrix1 matrix2 _______ _______ 5.3 a 2.2 b 3.3 a 4.4 a 1.1 b
writetable(matrix3, 'subject.dat', 'filetype', 'text', 'writevariablename', false, 'delimiter', 'tab')
type subject.dat
5.3 a 2.2 b 3.3 a 4.4 a 1.1 b

댓글 수: 3

Hi Mr. Roberson,
Thank you for answering my question. I just tested out the code you sent to see if it worked on my end, and it did not. I first thought I may have miss-written it, so I copied and pasted your code onto my code, and the current error that pops up is "Unsupported type 'cell'. Use writecell instead."
I tried writecell, but that also did not work. I am confused on how it worked on your end but not on mine. Here is what is looks like.
writetable(matrix3, 'subjex.dat', 'filetype', ...
'text', 'writevariablename', false, ...
'delimiter', 'tab')
type subjex.dat
The file name is subjex.m so I changed the 'subject.dat' to 'subjex.dat' hoping that would eliminate the issue.
Perhaps your actual code is doing something different than the test code. The output in matrix3 is, like the output says, a 5 x 2 table object, not a cell.
The code I showed works even if matrix2 is a cell array of character vectors.
a = 'apple';
b = 'ball';
matrix1 = [5.3, 2.2, 3.3, 4.4, 1.1]'
matrix1 = 5×1
5.3000 2.2000 3.3000 4.4000 1.1000
matrix2 = {a,b,a,a,b}'
matrix2 = 5×1 cell array
{'apple'} {'ball' } {'apple'} {'apple'} {'ball' }
matrix3 = table(matrix1,matrix2)
matrix3 = 5×2 table
matrix1 matrix2 _______ _________ 5.3 {'apple'} 2.2 {'ball' } 3.3 {'apple'} 4.4 {'apple'} 1.1 {'ball' }
writetable(matrix3, 'subject.dat', 'filetype', 'text', 'writevariablename', false, 'delimiter', 'tab')
type subject.dat
5.3 apple 2.2 ball 3.3 apple 4.4 apple 1.1 ball
MADISON RAGONE
MADISON RAGONE 2021년 12월 10일
Oh, it looks like I forgot the table function. Sorry about that. Thank you for taking the time to explain this.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2021년 12월 10일

댓글:

2021년 12월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by