combine variables into a multi column text file

I'm new to Matlab and trying to combine 3 variables into one text file, with each variable making up a column. I've tried using fprintf, but haven't had any luck.
The variables are:
row =
125
421
col =
651
876
name =
point_1
point_2
I want a text file which shows 3 columns like this:
651 125 point_1
876 421 point_2
Has anyone got any ideas how I can do this easily?
I need to this within an existing matlab script, as the text file is then used by the script to do other processing.
Thanks!

回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2013 年 5 月 6 日
編集済み: Andrei Bobrov 2013 年 5 月 6 日

0 投票

use cell arrays:
row = [
125
421];
col =[
651
876];
name = {
'point_1'
'point_2'};
datacell = [num2cell([row,col]), name];
or
struct arrays:
datastruct = struct('row',row,'col',col,'name',name);
Sarah
Sarah 2013 年 5 月 6 日

0 投票

Thanks, I've tried to use both formats and they work, but I haven't been able to write either of them to a text file. I've tried:
datacell = [num2cell([row,col]),name];
fid = fopen('points.txt','w');
fprintf(fid,'%d %d %s',datacell);
but this doesn't work and I can't work out why.

カテゴリ

ヘルプ センター および File ExchangeVariables についてさらに検索

タグ

質問済み:

2013 年 5 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by