Num2Str and concatenating help

I need to concatenate a cell array where 2 columns are strings and one is numeric. I tried doing this by first changing the numeric to num2str then to char but num2str makes it 4xn, n being how many digits the number has, which does not fit in with the dimentions of the rest(4x1). How can I fit this?
This is all of my code by the way:
function varargout = ymv2102_final(varargin)
% YMV2102_FINAL MATLAB code for ymv2102_final.fig
% YMV2102_FINAL, by itself, creates a new YMV2102_FINAL or raises the existing
% singleton*.
%
% H = YMV2102_FINAL returns the handle to a new YMV2102_FINAL or the handle to
% the existing singleton*.
%
% YMV2102_FINAL('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in YMV2102_FINAL.M with the given input arguments.
%
% YMV2102_FINAL('Property','Value',...) creates a new YMV2102_FINAL or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before ymv2102_final_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to ymv2102_final_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help ymv2102_final
% Last Modified by GUIDE v2.5 15-Dec-2011 05:08:18
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ymv2102_final_OpeningFcn, ...
'gui_OutputFcn', @ymv2102_final_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before ymv2102_final is made visible.
function ymv2102_final_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to ymv2102_final (see VARARGIN)
% Choose default command line output for ymv2102_final
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes ymv2102_final wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = ymv2102_final_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox1
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in getcost_pushbutton1.
function getcost_pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to getcost_pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
idx=get(handles.listbox1,'Value');
cost = dlmread('locationcost.txt',',');
tonyc = dlmread('tonyc.txt',',');
tonyc = tonyc(idx,1);
fromnyc = dlmread('fromnyc.txt',',');
fromnyc = fromnyc(:,idx);
cost=cost(idx,idx);
name = importdata('locationnames.txt');
name = name(idx);
costl=cost;
[X,Y]=find(fromnyc==min(fromnyc));
itinerary_cost=fromnyc(X,Y);
itinerary_name=name(Y);
costl(:,Y)=NaN;
for i=2:size(costl,1)
[X,Y]=find(costl==min(costl(Y,:)));
itinerary_cost(i,1)=costl(X,Y);
itinerary_name(i)=name(Y);
costl(:,Y)=NaN;
end
itinerary_cost(size(costl,1)+1,1)=tonyc(Y,1);
total=sum(itinerary_cost);
total_cost=num2str(total);
set(handles.totalanswer,'String',sprintf('%s%d%s','$ ',total_cost,'.00'));
from_itinerary{1}='New York, New York';
size(itinerary_cost,1);
for i=2:size(itinerary_name,2)+1
from_itinerary{i,1}=char(itinerary_name(i-1));
end
for i=1:size(itinerary_name,2)
to_itinerary{i,1}=char(itinerary_name(i));
end
itinerary_cost = num2str(itinerary_cost)
for i=1:size(itinerary_name,2)
itinerary_costs{i,1}=char(itinerary_cost(i))
end
to_itinerary{size(itinerary_name,2)+1,1}='New York, New York';
ree=cat(2, from_itinerary, to_itinerary)
set(handles.uitable1,'Data', ree)

1 Comment

To make your question effective, I would suggest you come up with a small set of example data and show the relevant part of your code. Your description of the problem certainly sounds like it can be easily abstracted. Don't just dump all your code.

Sign in to comment.

Answers (2)

a={'abc','ef',34;'AB','CDEFG',45};
b=a.';
c=sprintf('%s%s%d\n',b{:})
Each place that you use num2str(), change the argument to a column vector. For example, change
total_cost=num2str(total);
to
total_cost=num2str(total(:));

Tags

Asked:

on 15 Dec 2011

Community Treasure Hunt

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

Start Hunting!