現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I have data that I want to put in a excel file from matlab. the data is coming from a text file that I read in using textscan, here is my sample code:
fileID = fopen('passReport.EO1_2013_107_193146');
c = textscan(fileID, '%s')
fclose(fileID);
data = celldisp(c);
I want to take the data in the variable "data" and create a excel file with it
採用された回答
The "xlswrite" command should do what you need:
help xlswrite
xlswrite allows you to control the file to which your data is written, the sheet within that file to which your data is written, and the specific cells to which your data is written. If a file with the name you provide does not already exist, a new one is created. Very handy function.
14 件のコメント
Cordelle
2013 年 7 月 1 日
when i tried to use the xlswrite function, an error message occured:
Error using celldisp
Too many output arguments.
Error in Untitled (line 11)
xlswrite(filename, A)
this is the code i used:
fileID = fopen('passReport.EO1_2013_107_193146');
c = textscan(fileID, '%s')
fclose(fileID);
A = celldisp(c)
filename = 'EO1_2013_107_193146.xlsx';
xlswrite(filename, A)
Cordelle
2013 年 7 月 1 日
Do you know How i could get the data from passReport.EO1_2013_107_193146 to a excel file that I would name, EO1_2013_107_193146.xlsx using matlab code
Your error looks to be coming not from the xlswrite call, but from the celldisp call. celldisp does not operate with an output meaning A = celldisp will not work.
Is there any reason that you cannot simply write c directly to your excel file, like so?
xlswrite(filename, c)
If you cannot, for example because the textscan call is not returning the data in the form you want, you will need to make some changes to the way the data is organized, but I don't think celldisp would be the way to do it.
Cordelle
2013 年 7 月 2 日
Sorry for the late response, but no the xlswrite(filename, c) creates an excel file with none of the data transferred to it. So, essentially it creates an empty excel file.
Image Analyst
2013 年 7 月 2 日
So why'd you accept the answer?
Cordelle
2013 年 7 月 2 日
I accepted it because Evan attempted to help me and he did answer the basic question correctly, which was to open a excel file from matlab. So, I thought i should accept Evan's answer to show gratitude.
Evan
2013 年 7 月 2 日
I appreciate it, but it would probably be best for your purposes if you kept the question unanswered until you get your code working. People on this forum will be less likely to read a question that is marked as "Answered" because they will assume that there is no more help needed. ;)
I think I may have reproduced the problem you say you're having. I have two sections of code below. The first results in a blank excel file like you mention. The second results in an excel file where cells A1 through A4 are each filled with a word. Both scripts read in a text file named "test.txt" that contains the line "This is a test."
Script One (Blank Excel Result):
fid = fopen('test.txt');
t = textscan(fid,'%s');
xlswrite('test_1.xlsx',t);
Script Two (Correct Excel Result):
fid = fopen('test.txt');
t = textscan(fid,'%s');
xlswrite('test_1.xlsx',t{:});
Does the first code mimic your problem? And, if so, does the second code fix it?
If it does, the reason for your problem is that the textscan call is not just reading in your data as a cell array, but a cell array inside a cell array. The data comes in as a single 1x1 cell array that contains, inside of it, a 4x1 cell array. In order to write the data inside, you have to extract the "nested" 4x1 cell array from outer 1x1 cell array. That's what the t{:} call is doing.
Cordelle
2013 年 7 月 2 日
yes, the first code mimics my problem and the second get the data to the excel; however, it puts all the data in the first column. I needed the layout of the data to look exactly the way it would look if i were to manual open passReport.EO1_2013_107_193146 with excel
I believe that has to do more with the way you're reading in your data using textscan and less with the way its being written into excel. I modified my text file a bit so that test.txt is now five rows, all containing 1:10. I was able to read it in using textscan and then insert it into cells A1:J5 using the below code:
fid = fopen('test.txt');
t = textscan(fid,'%d %d %d %d %d %d %d %d %d %d');
xlswrite('test_1.xlsx',[t{:}])
you were right it had something to do with the way i was reading the file, because now that i altered the number of character fields the code work perfectly fine for the first 852 rows of data, after the first 852 rows the layout of the data acts up again.
does xlswrite() have a limit of how many rows it writes?
Cordelle
2013 年 7 月 2 日
nevermind, it works. thank you
The row/column limit for recent versions of excel is much higher than 852 rows, so I don't think excel would be the cause of your problem. Also, I had no trouble writing a random cell array of size 1000x200 to an excel file using xlswrite, so I don't think xlswrite would be the culprit. It sounds like it has something to do with your text file or the way you're reading it in, but I really can't say for sure.
Evan
2013 年 7 月 3 日
Oops, it looks like I either missed your last comment or we both posted at around the same time. Glad you have it working!
その他の回答 (1 件)
Image Analyst
2013 年 7 月 2 日
You can't send c into xlswrite or else it will put one character per Excel cell. You have to put the string into a cell first.
fileID = fopen('passReport.EO1_2013_107_193146');
c = textscan(fileID, '%s')
fclose(fileID);
caC = {c}; % Stick string into a single cell.
xlswrite(XLFileName, caC, 'A1');
1 件のコメント
Thanks for the help, but a error message still occurred:
Error using xlswrite (line 220)
Error: Object returned error code: 0x800A03EC
Error in Untitled (line 14)
xlswrite(filename, caC, 'A1');
the data in the file is numeric data, do you think its not transferring to the excel file because we are trying to read a string?
カテゴリ
ヘルプ センター および File Exchange で Spreadsheets についてさらに検索
製品
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
